Tobias Geisler commited on
Commit
2c92afc
·
1 Parent(s): 2843934

update to using built in secret management and update interface

Browse files
Files changed (2) hide show
  1. app.py +42 -26
  2. requirements.txt +0 -1
app.py CHANGED
@@ -1,35 +1,51 @@
1
- import gradio as gr
2
  import openai
3
  import os
 
 
4
 
5
- # Fetch your OpenAI API key from an environment variable
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  if openai.api_key is None:
9
- raise ValueError("OPENAI_API_KEY environment variable not set.")
 
 
 
 
 
 
 
10
 
11
- def chat_with_gpt(system_message, user_message):
12
- conversation = f"System: {system_message}\nUser: {user_message}"
13
- response = openai.Completion.create(
 
 
14
  model="gpt-3.5-turbo",
15
- prompt=conversation,
16
- temperature=0.5,
17
- max_tokens=500,
18
- frequency_penalty=0.0,
19
- presence_penalty=0.0,
20
  )
21
- return response.choices[0].text.strip()
22
-
23
- # Define the interface
24
- iface = gr.Interface(
25
- fn=chat_with_gpt,
26
- inputs=[
27
- gr.inputs.Textbox(lines=2, placeholder="System Message Here..."),
28
- gr.inputs.Textbox(lines=5, placeholder="Your Message Here..."),
29
- ],
30
- outputs="text",
31
- title="Chat with GPT-3.5",
32
- description="This Gradio app lets you chat with OpenAI's GPT-3.5 model. Enter a system message for initial context, and then chat as you would with a human.",
33
- theme="default", # or "huggingface" for Hugging Face theme
34
- allow_flagging="never",
35
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
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