Tobias Geisler
commited on
Commit
·
2c92afc
1
Parent(s):
2843934
update to using built in secret management and update interface
Browse files- app.py +42 -26
- requirements.txt +0 -1
app.py
CHANGED
@@ -1,35 +1,51 @@
|
|
1 |
-
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
|
8 |
if openai.api_key is None:
|
9 |
-
raise ValueError("OPENAI_API_KEY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
model="gpt-3.5-turbo",
|
15 |
-
|
16 |
-
temperature=
|
17 |
-
max_tokens=500,
|
18 |
-
frequency_penalty=0.0,
|
19 |
-
presence_penalty=0.0,
|
20 |
)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import openai
|
2 |
import os
|
3 |
+
import gradio as gr
|
4 |
+
from openai import OpenAI
|
5 |
|
6 |
+
# Ensure the OPENAI_API_KEY environment variable is set
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
|
9 |
if openai.api_key is None:
|
10 |
+
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
|
11 |
+
|
12 |
+
client = OpenAI()
|
13 |
+
|
14 |
+
def chat_with_gpt(user_input, system_message, temperature, history):
|
15 |
+
if not history:
|
16 |
+
# If starting a new conversation, add the system message first
|
17 |
+
history = [{"role": "system", "content": system_message}]
|
18 |
|
19 |
+
# Append the latest user message
|
20 |
+
history.append({"role": "user", "content": user_input})
|
21 |
+
|
22 |
+
# Get response from GPT-3.5 Turbo
|
23 |
+
response = client.chat.completions.create(
|
24 |
model="gpt-3.5-turbo",
|
25 |
+
messages=history,
|
26 |
+
temperature=temperature
|
|
|
|
|
|
|
27 |
)
|
28 |
+
|
29 |
+
# Append the assistant's response
|
30 |
+
assistant_message = response.choices[0].message['content']
|
31 |
+
history.append({"role": "assistant", "content": assistant_message})
|
32 |
+
|
33 |
+
return history, history # Return updated history for both display and state
|
34 |
+
|
35 |
+
# Gradio interface
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown("### Chatte mit deinem Mini-Game")
|
38 |
+
with gr.Row():
|
39 |
+
system_message = gr.Textbox(value="Du bist ein dickköpfiger Bürokrat, der nicht hilfreich sein will.", label="Systemnachricht", placeholder="Gib hier die Systemnachricht ein...")
|
40 |
+
user_input = gr.Textbox(label="Deine Nachricht", placeholder="Gib hier deine Chatnachricht ein...")
|
41 |
+
temperature_slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Temperatur")
|
42 |
+
submit_button = gr.Button("Senden")
|
43 |
+
chat_container = gr.Chatbot(label="Chatverlauf")
|
44 |
+
history_state = gr.State([]) # Using Gradio State to maintain conversation history
|
45 |
+
|
46 |
+
submit_button.click(fn=chat_with_gpt,
|
47 |
+
inputs=[user_input, system_message, temperature_slider, history_state],
|
48 |
+
outputs=[chat_container, history_state])
|
49 |
+
|
50 |
+
# Launch the Gradio app
|
51 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,2 @@
|
|
1 |
-
os
|
2 |
openai
|
3 |
gradio
|
|
|
|
|
1 |
openai
|
2 |
gradio
|