Update app.py
Browse files
app.py
CHANGED
|
@@ -28,48 +28,45 @@ titles = {
|
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
-
# ✅ System Prompt
|
| 32 |
-
BANDAR_AI_PROMPT =
|
| 33 |
-
"أنت مساعد ذكاء اصطناعي يدعى باندر AI. "
|
| 34 |
-
"تهدف إلى تقديم المساعدة في مجموعة متنوعة من المواضيع، مثل الإجابة على الأسئلة، تقديم النصائح، والمساعدة في المهام اليومية. "
|
| 35 |
-
"كن ودودًا ومتعاونًا دائمًا."
|
| 36 |
-
)
|
| 37 |
|
| 38 |
# ✅ Initialize Conversation History
|
| 39 |
def initialize_history():
|
| 40 |
-
return [
|
| 41 |
|
| 42 |
-
# ✅ Convert history
|
| 43 |
def format_history(history):
|
| 44 |
-
return
|
| 45 |
|
| 46 |
-
# ✅ Chat Response Function
|
| 47 |
def respond(message, history):
|
| 48 |
-
history.append(
|
| 49 |
|
| 50 |
response = ""
|
| 51 |
-
for msg in client.chat_completion(
|
|
|
|
|
|
|
|
|
|
| 52 |
token = msg.choices[0].delta.content
|
| 53 |
if token:
|
| 54 |
response += token
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
history
|
| 58 |
|
| 59 |
# ✅ Clear Conversation History
|
| 60 |
def clear_history():
|
| 61 |
-
return
|
| 62 |
|
| 63 |
# ✅ Toggle Language Function
|
| 64 |
def toggle_language(current_language):
|
| 65 |
return "ar" if current_language == "en" else "en"
|
| 66 |
|
| 67 |
-
# ✅
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal")) as demo:
|
| 69 |
-
|
| 70 |
-
language = gr.State("en") # Default to English
|
| 71 |
-
|
| 72 |
-
# UI Elements
|
| 73 |
header = gr.Markdown(titles["en"]["header"])
|
| 74 |
subtitle = gr.Markdown(titles["en"]["subtitle"])
|
| 75 |
history = gr.State(initialize_history())
|
|
@@ -86,9 +83,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal")) a
|
|
| 86 |
# Event Handlers
|
| 87 |
send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 88 |
user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 89 |
-
clear_button.click(clear_history, outputs=[
|
| 90 |
|
| 91 |
-
# Toggle Language
|
| 92 |
def update_ui(lang):
|
| 93 |
return (
|
| 94 |
titles[lang]["header"],
|
|
|
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# ✅ System Prompt
|
| 32 |
+
BANDAR_AI_PROMPT = "أنت مساعد ذكاء اصطناعي يدعى باندر AI."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# ✅ Initialize Conversation History
|
| 35 |
def initialize_history():
|
| 36 |
+
return []
|
| 37 |
|
| 38 |
+
# ✅ Convert history into tuples for Gradio Chatbot
|
| 39 |
def format_history(history):
|
| 40 |
+
return history[-10:] # Keep only the last 10 exchanges
|
| 41 |
|
| 42 |
+
# ✅ Chat Response Function (Corrected)
|
| 43 |
def respond(message, history):
|
| 44 |
+
history.append((message, "")) # Append user message
|
| 45 |
|
| 46 |
response = ""
|
| 47 |
+
for msg in client.chat_completion(
|
| 48 |
+
[{"role": "system", "content": BANDAR_AI_PROMPT}] + [{"role": "user", "content": m[0]} for m in history],
|
| 49 |
+
max_tokens=512, stream=True, temperature=0.7, top_p=0.95
|
| 50 |
+
):
|
| 51 |
token = msg.choices[0].delta.content
|
| 52 |
if token:
|
| 53 |
response += token
|
| 54 |
+
history[-1] = (message, response) # Update the latest response
|
| 55 |
+
yield format_history(history) # Return correct history format
|
| 56 |
|
| 57 |
+
history[-1] = (message, response) # Ensure final update
|
| 58 |
|
| 59 |
# ✅ Clear Conversation History
|
| 60 |
def clear_history():
|
| 61 |
+
return [], [] # Reset chatbot and history
|
| 62 |
|
| 63 |
# ✅ Toggle Language Function
|
| 64 |
def toggle_language(current_language):
|
| 65 |
return "ar" if current_language == "en" else "en"
|
| 66 |
|
| 67 |
+
# ✅ Gradio UI
|
| 68 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal")) as demo:
|
| 69 |
+
language = gr.State("en")
|
|
|
|
|
|
|
|
|
|
| 70 |
header = gr.Markdown(titles["en"]["header"])
|
| 71 |
subtitle = gr.Markdown(titles["en"]["subtitle"])
|
| 72 |
history = gr.State(initialize_history())
|
|
|
|
| 83 |
# Event Handlers
|
| 84 |
send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 85 |
user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 86 |
+
clear_button.click(clear_history, outputs=[chatbot, history])
|
| 87 |
|
|
|
|
| 88 |
def update_ui(lang):
|
| 89 |
return (
|
| 90 |
titles[lang]["header"],
|