dr3ai
commited on
Commit
·
1224a9d
1
Parent(s):
c0dfc5d
Update space
Browse files
app.py
CHANGED
|
@@ -1,70 +1,174 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import logging
|
| 6 |
+
from typing import List, Dict, Generator
|
| 7 |
|
| 8 |
+
# تحميل المتغيرات من ملف .env
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
+
# إعداد التسجيل (logging)
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
class Chatbot:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
self.default_model = os.getenv("DEFAULT_MODEL", "openai/gpt-oss-20b")
|
| 18 |
+
self.default_system_message = os.getenv("DEFAULT_SYSTEM_MESSAGE", "You are a friendly Chatbot.")
|
| 19 |
+
self.default_max_tokens = int(os.getenv("DEFAULT_MAX_TOKENS", "512"))
|
| 20 |
+
self.default_temperature = float(os.getenv("DEFAULT_TEMPERATURE", "0.7"))
|
| 21 |
+
self.default_top_p = float(os.getenv("DEFAULT_TOP_P", "0.95"))
|
| 22 |
+
|
| 23 |
+
def respond(
|
| 24 |
+
self,
|
| 25 |
+
message: str,
|
| 26 |
+
history: List[Dict[str, str]],
|
| 27 |
+
system_message: str,
|
| 28 |
+
max_tokens: int,
|
| 29 |
+
temperature: float,
|
| 30 |
+
top_p: float,
|
| 31 |
+
hf_token: gr.OAuthToken,
|
| 32 |
+
) -> Generator[str, None, None]:
|
| 33 |
+
"""
|
| 34 |
+
للاستجابة للرسائل باستخدام نموذج Hugging Face
|
| 35 |
+
|
| 36 |
+
لمزيد من المعلومات حول دعم Inference API في `huggingface_hub`،
|
| 37 |
+
يرجى مراجعة الوثائق:
|
| 38 |
+
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 39 |
+
"""
|
| 40 |
+
try:
|
| 41 |
+
# استخدام التوكن من OAuth أو من متغير البيئة كاحتياطي
|
| 42 |
+
token = hf_token.token if hf_token else os.getenv("HF_TOKEN")
|
| 43 |
+
|
| 44 |
+
if not token:
|
| 45 |
+
yield "❌ خطأ: لم يتم توفير رمز مصادقة. يرجى تسجيل الدخول أو تعيين HF_TOKEN في ملف .env"
|
| 46 |
+
return
|
| 47 |
+
|
| 48 |
+
client = InferenceClient(token=token, model=self.default_model)
|
| 49 |
|
| 50 |
+
messages = [{"role": "system", "content": system_message}]
|
| 51 |
+
messages.extend(history)
|
| 52 |
+
messages.append({"role": "user", "content": message})
|
| 53 |
|
| 54 |
+
response = ""
|
| 55 |
|
| 56 |
+
for message_chunk in client.chat_completion(
|
| 57 |
+
messages,
|
| 58 |
+
max_tokens=max_tokens,
|
| 59 |
+
stream=True,
|
| 60 |
+
temperature=temperature,
|
| 61 |
+
top_p=top_p,
|
| 62 |
+
):
|
| 63 |
+
if message_chunk.choices and message_chunk.choices[0].delta.content:
|
| 64 |
+
token = message_chunk.choices[0].delta.content
|
| 65 |
+
response += token
|
| 66 |
+
yield response
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
error_msg = f"❌ حدث خطأ أثناء معالجة طلبك: {str(e)}"
|
| 70 |
+
logger.error(error_msg)
|
| 71 |
+
yield error_msg
|
| 72 |
|
| 73 |
+
def create_interface():
|
| 74 |
+
"""إنشاء واجهة Gradio"""
|
| 75 |
+
chatbot = Chatbot()
|
| 76 |
+
|
| 77 |
+
# الحصول على القيم الافتراضية من البيئة
|
| 78 |
+
default_system_message = os.getenv("DEFAULT_SYSTEM_MESSAGE", "You are a friendly Chatbot.")
|
| 79 |
+
default_max_tokens = int(os.getenv("DEFAULT_MAX_TOKENS", "512"))
|
| 80 |
+
default_temperature = float(os.getenv("DEFAULT_TEMPERATURE", "0.7"))
|
| 81 |
+
default_top_p = float(os.getenv("DEFAULT_TOP_P", "0.95"))
|
| 82 |
+
|
| 83 |
+
# واجهة الدردشة
|
| 84 |
+
chat_interface = gr.ChatInterface(
|
| 85 |
+
chatbot.respond,
|
| 86 |
+
type="messages",
|
| 87 |
+
title="Dr.X Chatbot - 🤗",
|
| 88 |
+
description="دردشة ذكية باستخدام نماذج Hugging Face",
|
| 89 |
+
additional_inputs=[
|
| 90 |
+
gr.Textbox(
|
| 91 |
+
value=default_system_message,
|
| 92 |
+
label="الرسالة النظامية",
|
| 93 |
+
info="تحدد سلوك المساعد"
|
| 94 |
+
),
|
| 95 |
+
gr.Slider(
|
| 96 |
+
minimum=1,
|
| 97 |
+
maximum=2048,
|
| 98 |
+
value=default_max_tokens,
|
| 99 |
+
step=1,
|
| 100 |
+
label="الحد الأقصى للرموز",
|
| 101 |
+
info="الحد الأقصى لعدد الرموز المُنشأة"
|
| 102 |
+
),
|
| 103 |
+
gr.Slider(
|
| 104 |
+
minimum=0.1,
|
| 105 |
+
maximum=4.0,
|
| 106 |
+
value=default_temperature,
|
| 107 |
+
step=0.1,
|
| 108 |
+
label="الدرجة الحرارية",
|
| 109 |
+
info="قيم أعلى = إجابات أكثر إبداعاً، قيم أقل = إجابات أكثر تركيزاً"
|
| 110 |
+
),
|
| 111 |
+
gr.Slider(
|
| 112 |
+
minimum=0.1,
|
| 113 |
+
maximum=1.0,
|
| 114 |
+
value=default_top_p,
|
| 115 |
+
step=0.05,
|
| 116 |
+
label="Top-p (الاستعينة النووية)",
|
| 117 |
+
info="قيم أعلى = تنوع أكثر في الإجابات"
|
| 118 |
+
),
|
| 119 |
+
],
|
| 120 |
+
examples=[
|
| 121 |
+
["أخبرني عن نفسك"],
|
| 122 |
+
["ما هو الذكاء الاصطناعي؟"],
|
| 123 |
+
["كيف يمكنني تعلم البرمجة؟"]
|
| 124 |
+
],
|
| 125 |
+
cache_examples=False
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
return chat_interface
|
| 129 |
|
| 130 |
+
def main():
|
| 131 |
+
"""الدالة الرئيسية لتشغيل التطبيق"""
|
| 132 |
+
# إنشاء الواجهة
|
| 133 |
+
demo = gr.Blocks(
|
| 134 |
+
title="Dr.X Chatbot",
|
| 135 |
+
theme=gr.themes.Soft(),
|
| 136 |
+
css=".gradio-container {background-color: #f0f8ff}"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
with demo:
|
| 140 |
+
with gr.Row():
|
| 141 |
+
gr.Markdown("# 🤖 Dr.X Chatbot")
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
with gr.Sidebar():
|
| 145 |
+
gr.LoginButton()
|
| 146 |
+
gr.Markdown("### الإعدادات")
|
| 147 |
+
gr.Markdown(f"**النموذج الافتراضي:** {os.getenv('DEFAULT_MODEL', 'openai/gpt-oss-20b')}")
|
| 148 |
+
gr.Markdown("### التعليمات")
|
| 149 |
+
gr.Markdown("""
|
| 150 |
+
1. سجل الدخول باستخدام حساب Hugging Face
|
| 151 |
+
2. اكتب رسالتك في حقل الإدخال
|
| 152 |
+
3. اضغط إرسال أو Enter
|
| 153 |
+
4. اضبط الإعدادات المتقدمة حسب الحاجة
|
| 154 |
+
""")
|
| 155 |
+
|
| 156 |
+
# عرض واجهة الدردشة
|
| 157 |
+
chat_interface = create_interface()
|
| 158 |
+
chat_interface.render()
|
| 159 |
+
|
| 160 |
+
# إعدادات التشغيل
|
| 161 |
+
server_port = int(os.getenv("PORT", "7860"))
|
| 162 |
+
server_name = os.getenv("SERVER_NAME", "0.0.0.0")
|
| 163 |
+
share = os.getenv("SHARE_APP", "False").lower() == "true"
|
| 164 |
+
|
| 165 |
+
# تشغيل التطبيق
|
| 166 |
+
demo.launch(
|
| 167 |
+
server_name=server_name,
|
| 168 |
+
server_port=server_port,
|
| 169 |
+
share=share,
|
| 170 |
+
favicon_path="https://huggingface.co/front/assets/huggingface_logo-noborder.svg"
|
| 171 |
+
)
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
+
main()
|