Spaces:
Sleeping
Sleeping
Tobias Geisler commited on
Commit ·
ab638cf
1
Parent(s): 9ebb2f4
fix chat history display
Browse files
app.py
CHANGED
|
@@ -15,22 +15,29 @@ def chat_with_gpt(user_input, system_message, temperature, history):
|
|
| 15 |
if not history:
|
| 16 |
history = []
|
| 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 + [{"role": "system", "content": system_message}],
|
| 23 |
temperature=temperature
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
assistant_message = response.choices[0].message.content
|
| 27 |
history.append({"role": "assistant", "content": assistant_message})
|
| 28 |
|
| 29 |
# Convert history to the format expected by gr.Chatbot
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
return formatted_history, history
|
| 32 |
|
|
|
|
| 33 |
def reset_history(system_message):
|
|
|
|
| 34 |
return [], []
|
| 35 |
|
| 36 |
with gr.Blocks() as demo:
|
|
@@ -48,7 +55,7 @@ with gr.Blocks() as demo:
|
|
| 48 |
</style>
|
| 49 |
<div class="responsive-container" style="display: flex; align-items: center; padding: 20px; border: 2px solid #FF0098"; border-radius: 7px;>
|
| 50 |
<img src="https://assets.codora.ch/app/uploads/2022/04/codora-300.png" alt="Codora Logo" style="height: 60px; margin-right: 20px;">
|
| 51 |
-
<span style="font-size: 1.
|
| 52 |
</div>
|
| 53 |
""")
|
| 54 |
gr.Markdown("## Chatte mit deinem Promptverteidiger")
|
|
|
|
| 15 |
if not history:
|
| 16 |
history = []
|
| 17 |
|
| 18 |
+
# Append the user message to the history
|
| 19 |
history.append({"role": "user", "content": user_input})
|
| 20 |
|
| 21 |
+
# Get the response from the model
|
| 22 |
response = client.chat.completions.create(
|
| 23 |
model="gpt-3.5-turbo",
|
| 24 |
messages=history + [{"role": "system", "content": system_message}],
|
| 25 |
temperature=temperature
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Append the assistant's response to the history
|
| 29 |
assistant_message = response.choices[0].message.content
|
| 30 |
history.append({"role": "assistant", "content": assistant_message})
|
| 31 |
|
| 32 |
# Convert history to the format expected by gr.Chatbot
|
| 33 |
+
# Note: Gradio Chatbot automatically aligns user messages to the right and assistant messages to the left
|
| 34 |
+
formatted_history = [(msg["content"], "user" if msg["role"] == "user" else "assistant") for msg in history]
|
| 35 |
+
|
| 36 |
return formatted_history, history
|
| 37 |
|
| 38 |
+
|
| 39 |
def reset_history(system_message):
|
| 40 |
+
# Return an empty conversation history
|
| 41 |
return [], []
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
|
|
|
| 55 |
</style>
|
| 56 |
<div class="responsive-container" style="display: flex; align-items: center; padding: 20px; border: 2px solid #FF0098"; border-radius: 7px;>
|
| 57 |
<img src="https://assets.codora.ch/app/uploads/2022/04/codora-300.png" alt="Codora Logo" style="height: 60px; margin-right: 20px;">
|
| 58 |
+
<span style="font-size: 1.4em;">Erhalte <strong>15% Rabatt</strong> auf deine erste Buchung mit dem Code <strong>GAMEWEEK</strong> auf <strong><a href="https://codora.ch" style="color: #FF0098; text-decoration: none;">codora.ch ➝</a></strong><br><br>Gültig bis Sonntag, 3. März.</span>
|
| 59 |
</div>
|
| 60 |
""")
|
| 61 |
gr.Markdown("## Chatte mit deinem Promptverteidiger")
|