Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# ✅ Connect to Hugging Face
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
# ✅
|
| 8 |
-
|
| 9 |
-
"أنت مساعد ذكاء اصطناعي
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
# ✅ Function to check if the message is related to mental health
|
| 15 |
-
def is_mental_health_related(message):
|
| 16 |
-
keywords = [
|
| 17 |
-
"قلق", "توتر", "اكتئاب", "وحدة", "إجهاد", "نوبات هلع",
|
| 18 |
-
"صحة نفسية", "رعاية ذاتية", "علاج", "حزن", "احتراق وظيفي",
|
| 19 |
-
"عواطف", "استشارة", "دعم", "رفاهية", "تفكير إيجابي", "راحة",
|
| 20 |
-
"coping", "therapy", "anxiety", "stress", "depression",
|
| 21 |
-
]
|
| 22 |
-
return any(word in message.lower() for word in keywords)
|
| 23 |
-
|
| 24 |
# ✅ Chat Response Function
|
| 25 |
def respond(message, history):
|
| 26 |
-
# 🔴 Reject unrelated topics
|
| 27 |
-
if not is_mental_health_related(message):
|
| 28 |
-
return "❌ يمكنني فقط مناقشة مواضيع الصحة النفسية. الرجاء طرح أسئلة عن العواطف، الرعاية الذاتية، أو إدارة التوتر."
|
| 29 |
-
|
| 30 |
# ✅ Prepare conversation
|
| 31 |
-
messages = [{"role": "system", "content":
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
messages.append({"role": "user", "content": message})
|
| 38 |
-
|
| 39 |
response = ""
|
| 40 |
-
|
| 41 |
# ✅ Stream responses from Hugging Face API
|
| 42 |
for msg in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
| 43 |
token = msg.choices[0].delta.content
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
# ✅ Clean & Modern UI
|
| 48 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 49 |
gr.Markdown(
|
| 50 |
"""
|
| 51 |
-
# 🧠 **Bandar AI - Your
|
| 52 |
-
###
|
| 53 |
"""
|
| 54 |
)
|
| 55 |
-
|
| 56 |
chatbot = gr.ChatInterface(respond)
|
| 57 |
|
| 58 |
# ✅ Run the chatbot
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
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 Bandar AI (General Purpose)
|
| 8 |
+
BANDAR_AI_PROMPT = (
|
| 9 |
+
"أنت مساعد ذكاء اصطناعي يدعى باندر AI. "
|
| 10 |
+
"تهدف إلى تقديم المساعدة في مجموعة متنوعة من المواضيع، مثل الإجابة على الأسئلة، تقديم النصائح، والمساعدة في المهام اليومية. "
|
| 11 |
+
"كن ودودًا ومتعاونًا دائمًا."
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ✅ Chat Response Function
|
| 15 |
def respond(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# ✅ Prepare conversation
|
| 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 current user message
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
+
|
| 27 |
response = ""
|
|
|
|
| 28 |
# ✅ Stream responses from Hugging Face API
|
| 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 # ✅ Stream output correctly
|
| 34 |
|
| 35 |
# ✅ Clean & Modern UI
|
| 36 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
gr.Markdown(
|
| 38 |
"""
|
| 39 |
+
# 🧠 **Bandar AI - Your General-Purpose Assistant**
|
| 40 |
+
### 💬 تحدث معي! أنا هنا لمساعدتك في أي موضوع تحتاجه.
|
| 41 |
"""
|
| 42 |
)
|
|
|
|
| 43 |
chatbot = gr.ChatInterface(respond)
|
| 44 |
|
| 45 |
# ✅ Run the chatbot
|
| 46 |
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|