Bndo commited on
Commit
840e990
·
verified ·
1 Parent(s): 338342f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -68
app.py CHANGED
@@ -1,47 +1,19 @@
1
- import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
  # ✅ Connect to Hugging Face API (Replace with your model or API key)
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
- # ✅ Language Options
8
- titles = {
9
- "en": {
10
- "header": "🧠 Bandar AI - Your Smart Assistant",
11
- "subtitle": "💬 Talk to me! I'm here to help you with anything you need.",
12
- "chat_label": "Bandar AI",
13
- "input_label": "Type your message here",
14
- "placeholder": "Hello! How can I help you?",
15
- "send_button": "Send",
16
- "clear_button": "Clear Chat",
17
- "toggle_button": "Switch to Arabic"
18
- },
19
- "ar": {
20
- "header": "🧠 بندر AI - مساعدك الذكي",
21
- "subtitle": "💬 تحدث معي! أنا هنا لمساعدتك في أي موضوع تحتاجه.",
22
- "chat_label": "بندر AI",
23
- "input_label": "اكتب رسالتك هنا",
24
- "placeholder": "مرحبًا! كيف يمكنني مساعدتك؟",
25
- "send_button": "إرسال",
26
- "clear_button": "مسح المحادثة",
27
- "toggle_button": "التبديل إلى الإنجليزية"
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(
@@ -51,58 +23,33 @@ def respond(message, history):
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())
73
- chatbot = gr.Chatbot(label=titles["en"]["chat_label"], bubble_full_width=False)
74
-
75
  with gr.Row():
76
- user_input = gr.Textbox(label=titles["en"]["input_label"], placeholder=titles["en"]["placeholder"], scale=8)
77
- send_button = gr.Button(titles["en"]["send_button"], variant="primary", scale=2)
78
 
79
  with gr.Row():
80
- clear_button = gr.Button(titles["en"]["clear_button"], variant="secondary")
81
- toggle_button = gr.Button(titles["en"]["toggle_button"], variant="secondary")
82
 
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"],
91
- titles[lang]["subtitle"],
92
- titles[lang]["chat_label"],
93
- titles[lang]["input_label"],
94
- titles[lang]["placeholder"],
95
- titles[lang]["send_button"],
96
- titles[lang]["clear_button"],
97
- titles[lang]["toggle_button"]
98
- )
99
-
100
- toggle_button.click(toggle_language, inputs=[language], outputs=[language])
101
- toggle_button.click(
102
- update_ui, inputs=[language],
103
- outputs=[header, subtitle, chatbot, user_input, user_input, send_button, clear_button, toggle_button]
104
- )
105
-
106
  # ✅ Run the Chatbot
107
  if __name__ == "__main__":
108
  demo.launch()
 
1
+ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
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 AI Behavior
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  BANDAR_AI_PROMPT = "أنت مساعد ذكاء اصطناعي يدعى باندر AI."
9
 
10
  # ✅ Initialize Conversation History
11
  def initialize_history():
12
+ return [] # Start with an empty conversation history
 
 
 
 
13
 
14
+ # ✅ Chat Response Function (Fixed)
15
  def respond(message, history):
16
+ history.append((message, "")) # Append user message with empty response
17
 
18
  response = ""
19
  for msg in client.chat_completion(
 
23
  token = msg.choices[0].delta.content
24
  if token:
25
  response += token
26
+ history[-1] = (message, response) # Update last response
27
+ yield history # Send updated conversation history
28
 
29
+ history[-1] = (message, response) # Ensure full response is saved
30
 
31
  # ✅ Clear Conversation History
32
  def clear_history():
33
  return [], [] # Reset chatbot and history
34
 
 
 
 
 
35
  # ✅ Gradio UI
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)
43
+ send_button = gr.Button("Send", variant="primary", scale=2)
44
 
45
  with gr.Row():
46
+ clear_button = gr.Button("Clear Chat")
 
47
 
48
  # Event Handlers
49
  send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
50
  user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
51
  clear_button.click(clear_history, outputs=[chatbot, history])
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # ✅ Run the Chatbot
54
  if __name__ == "__main__":
55
  demo.launch()