Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
llm_api_options = ["OpenAI API","Azure OpenAI API","Google PaLM API", "Llama 2"]
|
9 |
+
TEST_MESSAGE = "My favorite TV shows are The Mentalist, The Blacklist, Designated Survivor, and Unforgettable. What are ten series that I should watch next?"
|
10 |
+
|
11 |
+
|
12 |
+
def test_handler(optionSelection, prompt: str = "Write an introductory paragraph to explain Generative AI to the reader of this content."):
|
13 |
+
if optionSelection not in llm_api_options:
|
14 |
+
raise ValueError("Invalid choice!")
|
15 |
+
|
16 |
+
match optionSelection:
|
17 |
+
case "OpenAI API":
|
18 |
+
#model = "gpt-35-turbo"
|
19 |
+
model = "gpt-4"
|
20 |
+
system_prompt: str = "Explain in detail to help student understand the concept.",
|
21 |
+
assistant_prompt: str = None,
|
22 |
+
|
23 |
+
messages = [
|
24 |
+
{"role": "user", "content": f"{prompt}"},
|
25 |
+
{"role": "system", "content": f"{system_prompt}"},
|
26 |
+
{"role": "assistant", "content": f"{assistant_prompt}"}
|
27 |
+
]
|
28 |
+
|
29 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
30 |
+
openai.api_version = '2020-11-07'
|
31 |
+
|
32 |
+
completion = openai.ChatCompletion.create(
|
33 |
+
model = model,
|
34 |
+
messages = messages,
|
35 |
+
temperature = 0.7
|
36 |
+
)
|
37 |
+
|
38 |
+
print(completion)
|
39 |
+
response = completion["choices"][0]["message"].content
|
40 |
+
print(response)
|
41 |
+
return message, response
|
42 |
+
case "Azure OpenAI API":
|
43 |
+
return "", ""
|
44 |
+
case "Google PaLM API":
|
45 |
+
return "", ""
|
46 |
+
case "Llama 2":
|
47 |
+
return "", ""
|
48 |
+
|
49 |
+
|
50 |
+
with gr.Blocks() as LLMDemoTabbedScreen:
|
51 |
+
with gr.Tab("Text-to-Text (Text Completion)"):
|
52 |
+
llm_selection = gr.Radio(llm_api_options, label="Select one", info="Which service do you want to use?", value="OpenAI API")
|
53 |
+
test_string = gr.Textbox(label="Try String", value=TEST_MESSAGE, lines=2)
|
54 |
+
test_string_response = gr.Textbox(label="Response")
|
55 |
+
test_string_output_info = gr.Label(value="Output Info", label="Info")
|
56 |
+
test_button = gr.Button("Try it")
|
57 |
+
|
58 |
+
test_button.click(
|
59 |
+
fn=test_handler,
|
60 |
+
inputs=[llm_api_options, test_string],
|
61 |
+
outputs=[test_string_output_info, test_string_response]
|
62 |
+
)
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
LLMDemoTabbedScreen.launch()
|