Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,6 @@ def level_to_prompt(level):
|
|
| 15 |
# Custom background CSS
|
| 16 |
css = """
|
| 17 |
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP&family=Playfair+Display&display=swap');
|
| 18 |
-
|
| 19 |
body {
|
| 20 |
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/67351c643fe51cb1aa28f2e5/wuyd5UYTh9jPrMJGmV9yC.jpeg');
|
| 21 |
background-size: cover;
|
|
@@ -58,25 +57,38 @@ def respond(message, history, level, max_tokens, temperature, top_p):
|
|
| 58 |
system_message = level_to_prompt(level)
|
| 59 |
messages = [{"role": "system", "content": system_message}]
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
messages.append({"role": "user", "content": message})
|
| 67 |
-
|
|
|
|
| 68 |
response = ""
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# UI layout
|
| 77 |
with gr.Blocks(css=css) as demo:
|
| 78 |
gr.Markdown("French Tutor", elem_id="custom-title")
|
| 79 |
-
|
| 80 |
with gr.Column(elem_id="chat-panel"):
|
| 81 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 82 |
level = gr.Dropdown(
|
|
@@ -95,4 +107,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
demo.launch()
|
|
|
|
| 15 |
# Custom background CSS
|
| 16 |
css = """
|
| 17 |
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP&family=Playfair+Display&display=swap');
|
|
|
|
| 18 |
body {
|
| 19 |
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/67351c643fe51cb1aa28f2e5/wuyd5UYTh9jPrMJGmV9yC.jpeg');
|
| 20 |
background-size: cover;
|
|
|
|
| 57 |
system_message = level_to_prompt(level)
|
| 58 |
messages = [{"role": "system", "content": system_message}]
|
| 59 |
|
| 60 |
+
# Handle history based on its format
|
| 61 |
+
if history and isinstance(history[0], dict):
|
| 62 |
+
# New format (messages with role/content)
|
| 63 |
+
messages.extend(history)
|
| 64 |
+
else:
|
| 65 |
+
# Old format (tuples)
|
| 66 |
+
for user, bot in history:
|
| 67 |
+
if user:
|
| 68 |
+
messages.append({"role": "user", "content": user})
|
| 69 |
+
if bot:
|
| 70 |
+
messages.append({"role": "assistant", "content": bot})
|
| 71 |
+
|
| 72 |
+
# Add current message
|
| 73 |
messages.append({"role": "user", "content": message})
|
| 74 |
+
|
| 75 |
+
# Generate response
|
| 76 |
response = ""
|
| 77 |
+
try:
|
| 78 |
+
for msg in client.chat_completion(
|
| 79 |
+
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
| 80 |
+
):
|
| 81 |
+
token = msg.choices[0].delta.content
|
| 82 |
+
if token is not None: # Handle None tokens
|
| 83 |
+
response += token
|
| 84 |
+
yield response
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error in chat completion: {e}")
|
| 87 |
+
yield f"Désolé! There was an error: {str(e)}"
|
| 88 |
|
| 89 |
# UI layout
|
| 90 |
with gr.Blocks(css=css) as demo:
|
| 91 |
gr.Markdown("French Tutor", elem_id="custom-title")
|
|
|
|
| 92 |
with gr.Column(elem_id="chat-panel"):
|
| 93 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 94 |
level = gr.Dropdown(
|
|
|
|
| 107 |
)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
+
demo.launch()
|