Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,3 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 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 |
-
|
| 23 |
-
response = ""
|
| 24 |
-
for msg in client.chat_completion(
|
| 25 |
-
[{"role": "system", "content": BANDAR_AI_PROMPT}] + [{"role": "user", "content": m[0]} for m in history],
|
| 26 |
-
max_tokens=512, stream=True, temperature=0.7, top_p=0.95
|
| 27 |
-
):
|
| 28 |
-
token = msg.choices[0].delta.content
|
| 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():
|
| 38 |
-
return [], [] # Reset chatbot and history
|
| 39 |
-
|
| 40 |
-
# ✅ Gradio UI
|
| 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)
|
| 48 |
-
send_button = gr.Button("Send", variant="primary", scale=2)
|
| 49 |
-
|
| 50 |
-
with gr.Row():
|
| 51 |
-
clear_button = gr.Button("Clear Chat")
|
| 52 |
-
|
| 53 |
-
# Event Handlers
|
| 54 |
-
send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 55 |
-
user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
|
| 56 |
-
clear_button.click(clear_history, outputs=[chatbot, history])
|
| 57 |
-
|
| 58 |
-
# ✅ Run the Chatbot
|
| 59 |
-
if __name__ == "__main__":
|
| 60 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
gr.load("models/HuggingFaceH4/zephyr-7b-beta").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|