Spaces:
Sleeping
Sleeping
Tobias Geisler
commited on
Commit
·
c0eacb2
1
Parent(s):
ebdb2a1
fix error, add history reset button
Browse files
app.py
CHANGED
|
@@ -13,26 +13,28 @@ 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 |
-
|
| 30 |
-
assistant_message = response.choices[0].message.content # Updated to correctly access the content attribute
|
| 31 |
history.append({"role": "assistant", "content": assistant_message})
|
| 32 |
-
|
| 33 |
-
return history, history # Return updated history for both display and state
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("### Chatte mit deinem Promptverteidiger")
|
| 38 |
with gr.Row():
|
|
@@ -40,12 +42,11 @@ with gr.Blocks() as demo:
|
|
| 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.5, label="Temperatur")
|
| 42 |
submit_button = gr.Button("Senden")
|
|
|
|
| 43 |
chat_container = gr.Chatbot(label="Chatverlauf")
|
| 44 |
-
history_state = gr.State([])
|
| 45 |
|
| 46 |
-
submit_button.click(fn=chat_with_gpt,
|
| 47 |
-
|
| 48 |
-
outputs=[chat_container, history_state])
|
| 49 |
|
| 50 |
-
# Launch the Gradio app
|
| 51 |
demo.launch()
|
|
|
|
| 13 |
|
| 14 |
def chat_with_gpt(user_input, system_message, temperature, history):
|
| 15 |
if not history:
|
|
|
|
| 16 |
history = [{"role": "system", "content": system_message}]
|
| 17 |
+
|
|
|
|
| 18 |
history.append({"role": "user", "content": user_input})
|
| 19 |
|
|
|
|
| 20 |
response = client.chat.completions.create(
|
| 21 |
model="gpt-3.5-turbo",
|
| 22 |
messages=history,
|
| 23 |
temperature=temperature
|
| 24 |
)
|
| 25 |
|
| 26 |
+
assistant_message = response.choices[0].message.content
|
|
|
|
| 27 |
history.append({"role": "assistant", "content": assistant_message})
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
formatted_history = [(msg["content"], msg["role"]) for msg in history]
|
| 30 |
+
return formatted_history, history
|
| 31 |
+
|
| 32 |
+
def reset_history(system_message):
|
| 33 |
+
# Start a new conversation with only the system message
|
| 34 |
+
new_history = [{"role": "system", "content": system_message}]
|
| 35 |
+
formatted_history = [(msg["content"], msg["role"]) for msg in new_history]
|
| 36 |
+
return formatted_history, new_history
|
| 37 |
+
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
gr.Markdown("### Chatte mit deinem Promptverteidiger")
|
| 40 |
with gr.Row():
|
|
|
|
| 42 |
user_input = gr.Textbox(label="Deine Nachricht", placeholder="Gib hier deine Chatnachricht ein...")
|
| 43 |
temperature_slider = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.5, label="Temperatur")
|
| 44 |
submit_button = gr.Button("Senden")
|
| 45 |
+
reset_button = gr.Button("Chatverlauf zurücksetzen")
|
| 46 |
chat_container = gr.Chatbot(label="Chatverlauf")
|
| 47 |
+
history_state = gr.State([])
|
| 48 |
|
| 49 |
+
submit_button.click(fn=chat_with_gpt, inputs=[user_input, system_message, temperature_slider, history_state], outputs=[chat_container, history_state])
|
| 50 |
+
reset_button.click(fn=reset_history, inputs=system_message, outputs=[chat_container, history_state]) # Reset click action
|
|
|
|
| 51 |
|
|
|
|
| 52 |
demo.launch()
|