Update app.py
Browse files
app.py
CHANGED
|
@@ -11,33 +11,33 @@ BANDAR_AI_PROMPT = (
|
|
| 11 |
"كن ودودًا ومتعاونًا دائمًا."
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
# ✅ Chat Response Function
|
| 15 |
-
def respond(message
|
| 16 |
-
#
|
| 17 |
-
messages = [{"role": "system", "content": BANDAR_AI_PROMPT}]
|
| 18 |
-
|
| 19 |
-
# Add previous conversation history
|
| 20 |
-
for user_msg, bot_msg in history:
|
| 21 |
-
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 22 |
-
if bot_msg: messages.append({"role": "assistant", "content": bot_msg})
|
| 23 |
|
| 24 |
-
# Add the
|
| 25 |
-
|
| 26 |
|
|
|
|
| 27 |
response = ""
|
| 28 |
-
|
| 29 |
-
for msg in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
| 30 |
token = msg.choices[0].delta.content
|
| 31 |
if token:
|
| 32 |
response += token
|
| 33 |
-
yield response #
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# ✅ Clean & Modern UI
|
| 36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
-
# 🧠 **Bandar AI - Your
|
| 40 |
-
### 💬 تحدث معي! أنا
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
chatbot = gr.ChatInterface(respond)
|
|
|
|
| 11 |
"كن ودودًا ومتعاونًا دائمًا."
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# ✅ Initialize Conversation History (Stateful Interaction)
|
| 15 |
+
conversation_history = [{"role": "system", "content": BANDAR_AI_PROMPT}]
|
| 16 |
+
|
| 17 |
# ✅ Chat Response Function
|
| 18 |
+
def respond(message):
|
| 19 |
+
global conversation_history # Use global variable to maintain state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Add the user's message to the conversation history
|
| 22 |
+
conversation_history.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
+
# Generate a response from the model
|
| 25 |
response = ""
|
| 26 |
+
for msg in client.chat_completion(conversation_history, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
|
|
|
| 27 |
token = msg.choices[0].delta.content
|
| 28 |
if token:
|
| 29 |
response += token
|
| 30 |
+
yield response # Stream the response
|
| 31 |
+
|
| 32 |
+
# Add the assistant's response to the conversation history
|
| 33 |
+
conversation_history.append({"role": "assistant", "content": response})
|
| 34 |
|
| 35 |
# ✅ Clean & Modern UI
|
| 36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
+
# 🧠 **Bandar AI - Your Stateful Assistant**
|
| 40 |
+
### 💬 تحدث معي! أنا أتذكر ما قلته سابقًا لجعل المحادثة أكثر طبيعية.
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
chatbot = gr.ChatInterface(respond)
|