Spaces:
Sleeping
Sleeping
Commit
·
17b235b
1
Parent(s):
8505e75
feat(api): add api key input
Browse files
app.py
CHANGED
@@ -2,36 +2,36 @@ import openai
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
|
5 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
6 |
MODEL_NAME = "gpt-4"
|
7 |
-
|
8 |
-
# Function to send messages to GPT-4
|
9 |
global_chat_history = ""
|
10 |
|
11 |
-
def generate_response(prompt):
|
12 |
-
global global_chat_history
|
13 |
-
# Send message to GPT-4
|
14 |
-
message = {"role": "user", "content": prompt}
|
15 |
-
chat_history_messages = [{"role": "user", "content": m} for m in global_chat_history.split("\n")] + [message]
|
16 |
-
response = openai.ChatCompletion.create(
|
17 |
-
model=MODEL_NAME,
|
18 |
-
messages=chat_history_messages,
|
19 |
-
temperature=1,
|
20 |
-
max_tokens=1024,
|
21 |
-
n=1,
|
22 |
-
stop=None,
|
23 |
-
)
|
24 |
-
# Update global chat history with the new message and response
|
25 |
-
global_chat_history += f"\nUser: {prompt}\nAI: {response.choices[0].message.content}"
|
26 |
-
return response.choices[0].message.content
|
27 |
-
|
28 |
-
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
blocks = gr.Interface(
|
33 |
fn=generate_response,
|
34 |
-
inputs=[
|
|
|
|
|
|
|
35 |
outputs=[gr.Textbox(placeholder="Response", lines=10, readonly=True)],
|
36 |
title="GPT-4 Chatbot",
|
37 |
theme="compact",
|
@@ -46,8 +46,9 @@ def add_cors_headers(response):
|
|
46 |
response.headers["Access-Control-Allow-Methods"] = "POST,OPTIONS"
|
47 |
return response
|
48 |
|
49 |
-
|
50 |
# @blocks.flask_app.after_request
|
|
|
|
|
51 |
def enable_cors(response):
|
52 |
return add_cors_headers(response)
|
53 |
|
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
|
|
|
5 |
MODEL_NAME = "gpt-4"
|
|
|
|
|
6 |
global_chat_history = ""
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
def generate_response(prompt, api_key):
|
10 |
+
global global_chat_history
|
11 |
+
openai.api_key = api_key
|
12 |
+
# Send message to GPT-4
|
13 |
+
message = {"role": "user", "content": prompt}
|
14 |
+
chat_history_messages = [{"role": "user", "content": m}
|
15 |
+
for m in global_chat_history.split("\n")] + [message]
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model=MODEL_NAME,
|
18 |
+
messages=chat_history_messages,
|
19 |
+
temperature=1,
|
20 |
+
max_tokens=1024,
|
21 |
+
n=1,
|
22 |
+
stop=None,
|
23 |
+
)
|
24 |
+
# Update global chat history with the new message and response
|
25 |
+
global_chat_history += f"\nUser: {prompt}\nAI: {response.choices[0].message.content}"
|
26 |
+
return response.choices[0].message.content
|
27 |
|
28 |
|
29 |
blocks = gr.Interface(
|
30 |
fn=generate_response,
|
31 |
+
inputs=[
|
32 |
+
gr.Textbox(placeholder="Enter your prompt...", lines=2),
|
33 |
+
gr.Textbox(label="API Key", type="password")
|
34 |
+
],
|
35 |
outputs=[gr.Textbox(placeholder="Response", lines=10, readonly=True)],
|
36 |
title="GPT-4 Chatbot",
|
37 |
theme="compact",
|
|
|
46 |
response.headers["Access-Control-Allow-Methods"] = "POST,OPTIONS"
|
47 |
return response
|
48 |
|
|
|
49 |
# @blocks.flask_app.after_request
|
50 |
+
|
51 |
+
|
52 |
def enable_cors(response):
|
53 |
return add_cors_headers(response)
|
54 |
|