Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,19 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
# ✅ Connect to Hugging Face API (Replace with your model or API key)
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
# ✅ System Prompt
|
| 8 |
BANDAR_AI_PROMPT = "أنت مساعد ذكاء اصطناعي يدعى باندر AI."
|
| 9 |
|
| 10 |
-
# ✅ Initialize
|
| 11 |
def initialize_history():
|
| 12 |
-
return [] #
|
| 13 |
|
| 14 |
-
# ✅
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def respond(message, history):
|
| 16 |
history.append((message, "")) # Append user message with empty response
|
| 17 |
|
|
@@ -24,9 +29,9 @@ def respond(message, history):
|
|
| 24 |
if token:
|
| 25 |
response += token
|
| 26 |
history[-1] = (message, response) # Update last response
|
| 27 |
-
yield history # Send
|
| 28 |
|
| 29 |
-
history[-1] = (message, response) # Ensure
|
| 30 |
|
| 31 |
# ✅ Clear Conversation History
|
| 32 |
def clear_history():
|
|
@@ -36,7 +41,7 @@ def clear_history():
|
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("# 🧠 Bandar AI - Chatbot")
|
| 38 |
history = gr.State(initialize_history())
|
| 39 |
-
chatbot = gr.Chatbot()
|
| 40 |
|
| 41 |
with gr.Row():
|
| 42 |
user_input = gr.Textbox(placeholder="Type your message here...", scale=8)
|
|
|
|
| 4 |
# ✅ Connect to Hugging Face API (Replace with your model or API key)
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# ✅ AI System Prompt
|
| 8 |
BANDAR_AI_PROMPT = "أنت مساعد ذكاء اصطناعي يدعى باندر AI."
|
| 9 |
|
| 10 |
+
# ✅ Initialize Empty Chat History
|
| 11 |
def initialize_history():
|
| 12 |
+
return [] # No dictionaries, only tuples later
|
| 13 |
|
| 14 |
+
# ✅ Ensure History is Properly Formatted
|
| 15 |
+
def format_history(history):
|
| 16 |
+
"""Ensure history is a list of (user, assistant) tuples."""
|
| 17 |
+
return [(msg[0], msg[1]) for msg in history if len(msg) == 2]
|
| 18 |
+
|
| 19 |
+
# ✅ Chat Response Function (Corrected)
|
| 20 |
def respond(message, history):
|
| 21 |
history.append((message, "")) # Append user message with empty response
|
| 22 |
|
|
|
|
| 29 |
if token:
|
| 30 |
response += token
|
| 31 |
history[-1] = (message, response) # Update last response
|
| 32 |
+
yield format_history(history) # ✅ Send history in correct tuple format
|
| 33 |
|
| 34 |
+
history[-1] = (message, response) # Ensure final response is saved
|
| 35 |
|
| 36 |
# ✅ Clear Conversation History
|
| 37 |
def clear_history():
|
|
|
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
gr.Markdown("# 🧠 Bandar AI - Chatbot")
|
| 43 |
history = gr.State(initialize_history())
|
| 44 |
+
chatbot = gr.Chatbot() # ✅ This will now receive tuples, not dictionaries
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
user_input = gr.Textbox(placeholder="Type your message here...", scale=8)
|