Update app.py
Browse files
app.py
CHANGED
@@ -1,123 +1,45 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import openai
|
4 |
-
import requests
|
5 |
-
import json
|
6 |
-
|
7 |
-
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
8 |
-
|
9 |
-
prompt_templates = {"Default ChatGPT": ""}
|
10 |
-
|
11 |
-
def get_empty_state():
|
12 |
-
return {"total_tokens": 0, "messages": []}
|
13 |
-
|
14 |
-
def download_prompt_templates():
|
15 |
-
url = "https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv"
|
16 |
-
response = requests.get(url)
|
17 |
-
|
18 |
-
for line in response.text.splitlines()[1:]:
|
19 |
-
act, prompt = line.split('","')
|
20 |
-
prompt_templates[act.replace('"', '')] = prompt.replace('"', '')
|
21 |
-
|
22 |
-
choices = list(prompt_templates.keys())
|
23 |
-
return gr.update(value=choices[0], choices=choices)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
def on_prompt_template_change(prompt_template):
|
29 |
-
if not isinstance(prompt_template, str): return
|
30 |
-
return prompt_templates[prompt_template]
|
31 |
-
|
32 |
-
def submit_message(user_token, prompt, prompt_template, temperature, max_tokens, state):
|
33 |
-
|
34 |
-
history = state['messages']
|
35 |
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
system_prompt = [{ "role": "system", "content": prompt_template }]
|
44 |
|
45 |
-
|
46 |
-
|
47 |
try:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
except Exception as e:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
#header {text-align: center;}
|
75 |
-
#prompt_template_preview {padding: 1em; border-width: 1px; border-style: solid; border-color: #e0e0e0; border-radius: 4px;}
|
76 |
-
#total_tokens_str {text-align: right; font-size: 0.8em; color: #666; height: 1em;}
|
77 |
-
#label {font-size: 0.8em; padding: 0.5em; margin: 0;}
|
78 |
-
.message { font-size: 1.2em; }
|
79 |
-
"""
|
80 |
-
|
81 |
-
with gr.Blocks(css=css) as demo:
|
82 |
-
|
83 |
-
state = gr.State(get_empty_state())
|
84 |
-
|
85 |
-
|
86 |
-
with gr.Column(elem_id="col-container"):
|
87 |
-
gr.Markdown("""## OpenAI ChatGPT Demo
|
88 |
-
Using the ofiicial API (gpt-3.5-turbo model)<br>
|
89 |
-
Prompt templates from [awesome-chatgpt-prompts](https://github.com/f/awesome-chatgpt-prompts).<br>
|
90 |
-
Current limit is 3000 tokens per conversation.""",
|
91 |
-
elem_id="header")
|
92 |
-
|
93 |
-
with gr.Row():
|
94 |
-
with gr.Column():
|
95 |
-
chatbot = gr.Chatbot(elem_id="chatbox")
|
96 |
-
input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True).style(container=False)
|
97 |
-
btn_submit = gr.Button("Submit")
|
98 |
-
total_tokens_str = gr.Markdown(elem_id="total_tokens_str")
|
99 |
-
btn_clear_conversation = gr.Button("🔃 Start New Conversation")
|
100 |
-
with gr.Column():
|
101 |
-
prompt_template = gr.Dropdown(label="Set a custom insruction for the chatbot:", choices=list(prompt_templates.keys()))
|
102 |
-
prompt_template_preview = gr.Markdown(elem_id="prompt_template_preview")
|
103 |
-
gr.Markdown("Enter your own OpenAI API Key to remove the 3000 token limit. You can get it [here](https://platform.openai.com/account/api-keys).", elem_id="label")
|
104 |
-
user_token = gr.Textbox(placeholder="OpenAI API Key", type="password", show_label=False)
|
105 |
-
with gr.Accordion("Advanced parameters", open=False):
|
106 |
-
temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, interactive=True, label="Temperature (higher = more creative/chaotic)")
|
107 |
-
max_tokens = gr.Slider(minimum=100, maximum=4096, value=1000, step=1, interactive=True, label="Max tokens per response")
|
108 |
-
|
109 |
-
gr.HTML('''<br><br><br><center><a href="https://huggingface.co/spaces/anzorq/chatgpt-demo?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>You can duplicate this Space.<br>
|
110 |
-
Don't forget to set your own <a href="https://platform.openai.com/account/api-keys">OpenAI API Key</a> environment variable in Settings.<br>
|
111 |
-
<p><img src="https://visitor-badge.glitch.me/badge?page_id=anzorq.chatgpt_api_demo_hf" alt="visitors"></p></center>''')
|
112 |
-
|
113 |
-
btn_submit.click(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state])
|
114 |
-
input_message.submit(submit_message, [user_token, input_message, prompt_template, temperature, max_tokens, state], [input_message, chatbot, total_tokens_str, state])
|
115 |
-
btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot, total_tokens_str, state])
|
116 |
-
prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview])
|
117 |
-
user_token.change(on_token_change, inputs=[user_token], outputs=[])
|
118 |
-
|
119 |
-
|
120 |
-
demo.load(download_prompt_templates, inputs=None, outputs=[prompt_template])
|
121 |
-
|
122 |
-
|
123 |
-
demo.launch(debug=True, height='800px')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
openai.ChatCompletion.create(
|
7 |
+
model="gpt-3.5-turbo",
|
8 |
|
9 |
+
# Set OpenAI API key from environment variable
|
10 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
11 |
|
12 |
+
# Define GPT-3.5 Turbo model
|
13 |
+
model = "gpt-3.5-turbo"
|
|
|
14 |
|
15 |
+
# Define function for generating responses
|
16 |
+
def generate_response(prompt):
|
17 |
try:
|
18 |
+
response = openai.Completion.create(
|
19 |
+
engine=model,
|
20 |
+
prompt=prompt,
|
21 |
+
max_tokens=1024,
|
22 |
+
n=1,
|
23 |
+
stop=None,
|
24 |
+
temperature=0.7,
|
25 |
+
)
|
26 |
+
return response.choices[0].text.strip()
|
27 |
except Exception as e:
|
28 |
+
return f"Error: {e}"
|
29 |
+
|
30 |
+
# Define input and output interfaces
|
31 |
+
input_text = gr.inputs.Textbox(label="Enter your message:")
|
32 |
+
output_text = gr.outputs.Textbox(label="AI response:")
|
33 |
+
|
34 |
+
# Define Gradio app
|
35 |
+
gradio_app = gr.Interface(
|
36 |
+
fn=generate_response,
|
37 |
+
inputs=input_text,
|
38 |
+
outputs=output_text,
|
39 |
+
title="GPT-3.5 Turbo Chatbot",
|
40 |
+
description="Enter a message and get a response from an AI chatbot powered by GPT-3.5 Turbo.",
|
41 |
+
theme="compact",
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch Gradio app
|
45 |
+
gradio_app.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|