Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,37 +15,34 @@ Use clear sections, shots, and visual descriptions when possible.
|
|
| 15 |
# -------------------------
|
| 16 |
# Chat Function (FIXED)
|
| 17 |
# -------------------------
|
| 18 |
-
def respond(message,
|
| 19 |
if not message or message.strip() == "":
|
| 20 |
return history
|
| 21 |
|
| 22 |
-
# Start conversation with system prompt
|
| 23 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
messages.append(
|
| 28 |
|
| 29 |
-
# Add new user message
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
assistant_reply = response.choices[0].message.content
|
| 41 |
|
| 42 |
-
|
| 43 |
-
history.append({"role": "user", "content": message})
|
| 44 |
-
history.append({"role": "assistant", "content": assistant_reply})
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# -------------------------
|
| 51 |
# Custom CSS
|
|
@@ -121,7 +118,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 121 |
# -------------------------
|
| 122 |
send.click(
|
| 123 |
respond,
|
| 124 |
-
inputs=[msg, chatbot
|
| 125 |
outputs=chatbot,
|
| 126 |
)
|
| 127 |
|
|
|
|
| 15 |
# -------------------------
|
| 16 |
# Chat Function (FIXED)
|
| 17 |
# -------------------------
|
| 18 |
+
def respond(message, history, model, temperature, max_tokens):
|
| 19 |
if not message or message.strip() == "":
|
| 20 |
return history
|
| 21 |
|
|
|
|
| 22 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 23 |
|
| 24 |
+
for user, assistant in history:
|
| 25 |
+
messages.append({"role": "user", "content": user})
|
| 26 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 27 |
|
|
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
+
try:
|
| 31 |
+
response = client.chat.completions.create(
|
| 32 |
+
model=model,
|
| 33 |
+
messages=messages,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
max_completion_tokens=max_tokens,
|
| 36 |
+
)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
assistant_reply = response.choices[0].message.content
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
history.append((message, assistant_reply))
|
| 41 |
+
return history
|
| 42 |
|
| 43 |
+
except Exception as e:
|
| 44 |
+
history.append((message, f"❌ Error: {str(e)}"))
|
| 45 |
+
return history
|
| 46 |
|
| 47 |
# -------------------------
|
| 48 |
# Custom CSS
|
|
|
|
| 118 |
# -------------------------
|
| 119 |
send.click(
|
| 120 |
respond,
|
| 121 |
+
inputs=[msg, chatbot, model, temperature, max_tokens],
|
| 122 |
outputs=chatbot,
|
| 123 |
)
|
| 124 |
|