Bndo commited on
Commit
0b9d49f
·
verified ·
1 Parent(s): e73b6a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -30
app.py CHANGED
@@ -1,73 +1,108 @@
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 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()
 
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 for Bandar AI
32
  BANDAR_AI_PROMPT = (
33
+ "أنت مساعد ذكاء اصطناعي يدعى باندر AI. "
34
  "تهدف إلى تقديم المساعدة في مجموعة متنوعة من المواضيع، مثل الإجابة على الأسئلة، تقديم النصائح، والمساعدة في المهام اليومية. "
35
  "كن ودودًا ومتعاونًا دائمًا."
36
  )
37
 
38
+ # ✅ Initialize Conversation History
39
  def initialize_history():
40
  return [{"role": "system", "content": BANDAR_AI_PROMPT}]
41
 
42
  # ✅ Chat Response Function
43
  def respond(message, history):
 
44
  history.append({"role": "user", "content": message})
45
 
 
46
  response = ""
47
  for msg in client.chat_completion(history, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
48
  token = msg.choices[0].delta.content
49
  if token:
50
  response += token
51
+ yield [(msg["content"], history[i + 1]["content"]) for i, msg in enumerate(history[:-1]) if msg["role"] == "user"] + [(message, response)]
52
 
 
53
  history.append({"role": "assistant", "content": response})
54
 
55
  # ✅ Clear Conversation History
56
  def clear_history():
57
+ return initialize_history(), []
58
+
59
+ # ✅ Toggle Language Function
60
+ def toggle_language(current_language):
61
+ return "ar" if current_language == "en" else "en"
62
 
63
  # ✅ Customized UI with Gradio Components
64
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal")) as demo:
65
+ # Language state
66
+ language = gr.State("en") # Default to English
 
 
 
 
67
 
68
+ # UI Elements
69
+ header = gr.Markdown(titles["en"]["header"])
70
+ subtitle = gr.Markdown(titles["en"]["subtitle"])
71
  history = gr.State(initialize_history())
72
+ chatbot = gr.Chatbot(label=titles["en"]["chat_label"], bubble_full_width=False)
73
+
 
 
 
74
  with gr.Row():
75
+ user_input = gr.Textbox(label=titles["en"]["input_label"], placeholder=titles["en"]["placeholder"], scale=8)
76
+ send_button = gr.Button(titles["en"]["send_button"], variant="primary", scale=2)
 
 
 
 
77
 
 
78
  with gr.Row():
79
+ clear_button = gr.Button(titles["en"]["clear_button"], variant="secondary")
80
+ toggle_button = gr.Button(titles["en"]["toggle_button"], variant="secondary")
81
+
82
  # Event Handlers
83
  send_button.click(respond, inputs=[user_input, history], outputs=[chatbot, history])
84
  user_input.submit(respond, inputs=[user_input, history], outputs=[chatbot, history])
85
  clear_button.click(clear_history, outputs=[history, chatbot])
86
 
87
+ # Toggle Language
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()