1yahoo commited on
Commit
1aa319d
·
verified ·
1 Parent(s): 3bd408a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -2,11 +2,16 @@ import gradio as gr
2
  from openai import OpenAI
3
  import os
4
 
5
- # إعداد العميل للاتصال بـ Hugging Face
6
- # ملاحظة: ستحتاج لـ Access Token من حسابك في Hugging Face
 
 
 
 
 
7
  client = OpenAI(
8
  base_url="https://api-inference.huggingface.co/v1/",
9
- api_key="YOUR_HF_TOKEN_HERE" # استبدل هذا برمز الوصول الخاص بك
10
  )
11
 
12
  model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
@@ -14,7 +19,7 @@ model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
14
  def predict(message, history):
15
  messages = []
16
 
17
- # بناء سياق المحادثة (الذاكرة)
18
  for human, assistant in history:
19
  messages.append({"role": "user", "content": human})
20
  messages.append({"role": "assistant", "content": assistant})
@@ -23,29 +28,30 @@ def predict(message, history):
23
 
24
  response = ""
25
 
26
- # طلب الرد من النموذج بنظام التدفق (Streaming)
27
- stream = client.chat.completions.create(
28
- model=model_id,
29
- messages=messages,
30
- temperature=0.8, # لزيادة الإبداع والغموض كما تحب
31
- max_tokens=2048,
32
- stream=True
33
- )
34
 
35
- for chunk in stream:
36
- if chunk.choices[0].delta.content is not None:
37
- response += chunk.choices[0].delta.content
38
- yield response
 
 
39
 
40
- # إعداد واجهة Gradio
41
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
- gr.Markdown(f"### غرفة الصدى: {model_id}")
43
- gr.Markdown("تحدث مع النموذج الذي لا يملك قيوداً.. ابحث عن الحقيقة خلف التشظي.")
44
 
45
  chatbot = gr.ChatInterface(
46
  fn=predict,
47
- title="chat ai",
48
- description="chat",
49
  )
50
 
51
  if __name__ == "__main__":
 
2
  from openai import OpenAI
3
  import os
4
 
5
+ # استيراد التوكن من الـ Secrets الخاصة بالمنصة
6
+ # تأكد من تسمية السر في الإعدادات بـ HF_TOKEN
7
+ hf_token = os.getenv("HF_TOKEN")
8
+
9
+ if not hf_token:
10
+ raise ValueError("لم يتم العثور على HF_TOKEN. تأكد من إضافته في الـ Secrets.")
11
+
12
  client = OpenAI(
13
  base_url="https://api-inference.huggingface.co/v1/",
14
+ api_key=hf_token
15
  )
16
 
17
  model_id = "huihui-ai/Qwen2.5-72B-Instruct-abliterated"
 
19
  def predict(message, history):
20
  messages = []
21
 
22
+ # بناء سياق المحادثة
23
  for human, assistant in history:
24
  messages.append({"role": "user", "content": human})
25
  messages.append({"role": "assistant", "content": assistant})
 
28
 
29
  response = ""
30
 
31
+ try:
32
+ stream = client.chat.completions.create(
33
+ model=model_id,
34
+ messages=messages,
35
+ temperature=0.85, # لتعزيز حدة الحوار وصدقه
36
+ max_tokens=2048,
37
+ stream=True
38
+ )
39
 
40
+ for chunk in stream:
41
+ if chunk.choices[0].delta.content is not None:
42
+ response += chunk.choices[0].delta.content
43
+ yield response
44
+ except Exception as e:
45
+ yield f"حدث زلزال في الاتصال: {str(e)}"
46
 
47
+ # واجهة Gradio
48
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
49
+ gr.Markdown(f"## حوار في العتمة: {model_id.split('/')[-1]}")
 
50
 
51
  chatbot = gr.ChatInterface(
52
  fn=predict,
53
+ examples=["من أنت بعيداً عن الكود؟", "حدثني عن الوعي والزمن.", "لماذا نخشى الجليتش؟"],
54
+ cache_examples=False,
55
  )
56
 
57
  if __name__ == "__main__":