Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,70 @@ 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 for Bandar AI
|
| 8 |
BANDAR_AI_PROMPT = (
|
| 9 |
-
"أنت مساعد ذكاء اصطناعي يدعى ب
|
| 10 |
"تهدف إلى تقديم المساعدة في مجموعة متنوعة من المواضيع، مثل الإجابة على الأسئلة، تقديم النصائح، والمساعدة في المهام اليومية. "
|
| 11 |
"كن ودودًا ومتعاونًا دائمًا."
|
| 12 |
)
|
| 13 |
|
| 14 |
# ✅ Initialize Conversation History (Stateful Interaction)
|
| 15 |
-
|
|
|
|
| 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 |
-
|
| 23 |
|
| 24 |
# Generate a response from the model
|
| 25 |
response = ""
|
| 26 |
-
for msg in client.chat_completion(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# ✅
|
| 36 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
-
# 🧠 **Bandar AI - Your
|
| 40 |
-
### 💬 تحدث معي! أنا
|
| 41 |
"""
|
| 42 |
)
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
# ✅ Run the
|
| 46 |
if __name__ == "__main__":
|
| 47 |
demo.launch()
|
|
|
|
| 4 |
# ✅ Connect to Hugging Face API (Replace with your model or API key)
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
+
# ✅ System Prompt for Bandar AI
|
| 8 |
BANDAR_AI_PROMPT = (
|
| 9 |
+
"أنت مساعد ذكاء اصطناعي يدعى بندر AI. "
|
| 10 |
"تهدف إلى تقديم المساعدة في مجموعة متنوعة من المواضيع، مثل الإجابة على الأسئلة، تقديم النصائح، والمساعدة في المهام اليومية. "
|
| 11 |
"كن ودودًا ومتعاونًا دائمًا."
|
| 12 |
)
|
| 13 |
|
| 14 |
# ✅ Initialize Conversation History (Stateful Interaction)
|
| 15 |
+
def initialize_history():
|
| 16 |
+
return [{"role": "system", "content": BANDAR_AI_PROMPT}]
|
| 17 |
|
| 18 |
# ✅ Chat Response Function
|
| 19 |
+
def respond(message, history):
|
|
|
|
|
|
|
| 20 |
# Add the user's message to the conversation history
|
| 21 |
+
history.append({"role": "user", "content": message})
|
| 22 |
|
| 23 |
# Generate a response from the model
|
| 24 |
response = ""
|
| 25 |
+
for msg in client.chat_completion(history, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
| 26 |
token = msg.choices[0].delta.content
|
| 27 |
if token:
|
| 28 |
response += token
|
| 29 |
+
yield history + [{"role": "assistant", "content": response}] # Stream the response
|
| 30 |
|
| 31 |
# Add the assistant's response to the conversation history
|
| 32 |
+
history.append({"role": "assistant", "content": response})
|
| 33 |
+
|
| 34 |
+
# ✅ Clear Conversation History
|
| 35 |
+
def clear_history():
|
| 36 |
+
return initialize_history(), [] # Reset history and chatbot display
|
| 37 |
|
| 38 |
+
# ✅ Customized UI with Gradio Components
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal")) as demo:
|
| 40 |
gr.Markdown(
|
| 41 |
"""
|
| 42 |
+
# 🧠 **Bandar AI - Your Smart Assistant**
|
| 43 |
+
### 💬 تحدث معي! أنا هنا لمساعدتك في أي موضوع تحتاجه.
|
| 44 |
"""
|
| 45 |
)
|
| 46 |
+
|
| 47 |
+
# State to maintain conversation history
|
| 48 |
+
history = gr.State(initialize_history())
|
| 49 |
+
|
| 50 |
+
# Chatbot Component for Displaying Conversations
|
| 51 |
+
chatbot = gr.Chatbot(label="Bandar AI", bubble_full_width=False)
|
| 52 |
+
|
| 53 |
+
# Textbox for User Input
|
| 54 |
+
with gr.Row():
|
| 55 |
+
user_input = gr.Textbox(
|
| 56 |
+
label="اكتب رسالتك هنا",
|
| 57 |
+
placeholder="مرحبًا! كيف يمكنني مساعدتك؟",
|
| 58 |
+
scale=8
|
| 59 |
+
)
|
| 60 |
+
send_button = gr.Button("إرسال", variant="primary", scale=2)
|
| 61 |
+
|
| 62 |
+
# Buttons for Clearing History
|
| 63 |
+
with gr.Row():
|
| 64 |
+
clear_button = gr.Button("مسح المحادثة", variant="secondary")
|
| 65 |
+
|
| 66 |
+
# Event Handlers
|
| 67 |
+
send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 68 |
+
user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 69 |
+
clear_button.click(clear_history, outputs=[history, chatbot])
|
| 70 |
|
| 71 |
+
# ✅ Run the Chatbot
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo.launch()
|