Alshargi commited on
Commit
6ef8de9
·
verified ·
1 Parent(s): c1c1071

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -9
app.py CHANGED
@@ -1,14 +1,61 @@
 
1
  import gradio as gr
 
2
 
3
- def hello(message):
4
- return f"Hadithi is working. You said: {message}"
5
 
6
- demo = gr.Interface(
7
- fn=hello,
8
- inputs=gr.Textbox(label="Ask"),
9
- outputs=gr.Textbox(label="Reply"),
10
- title="Hadithi",
11
- description="First test for the Hugging Face Space"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
 
14
- demo.launch()
 
 
1
+ import os
2
  import gradio as gr
3
+ from openai import OpenAI
4
 
5
+ HF_TOKEN = os.environ.get("HF_TOKEN")
6
+ MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
7
 
8
+ if not HF_TOKEN:
9
+ raise ValueError("HF_TOKEN is missing. Add it in Space Settings -> Secrets.")
10
+
11
+ client = OpenAI(
12
+ base_url="https://router.huggingface.co/v1",
13
+ api_key=HF_TOKEN,
14
+ )
15
+
16
+ SYSTEM_PROMPT = """
17
+ You are Hadithi AI, a helpful assistant.
18
+ Answer clearly, briefly, and in the same language as the user when possible.
19
+ If the user writes in Arabic, answer in Arabic.
20
+ If the user writes in English, answer in English.
21
+ """.strip()
22
+
23
+
24
+ def chat(message, history):
25
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
26
+
27
+ for user_msg, assistant_msg in history:
28
+ if user_msg:
29
+ messages.append({"role": "user", "content": user_msg})
30
+ if assistant_msg:
31
+ messages.append({"role": "assistant", "content": assistant_msg})
32
+
33
+ messages.append({"role": "user", "content": message})
34
+
35
+ try:
36
+ response = client.chat.completions.create(
37
+ model=MODEL_ID,
38
+ messages=messages,
39
+ temperature=0.3,
40
+ max_tokens=600,
41
+ )
42
+ answer = response.choices[0].message.content.strip()
43
+ except Exception as e:
44
+ answer = f"Error: {str(e)}"
45
+
46
+ return answer
47
+
48
+
49
+ demo = gr.ChatInterface(
50
+ fn=chat,
51
+ title="Hadithi AI",
52
+ description="Chat with Qwen2.5-7B-Instruct",
53
+ textbox=gr.Textbox(
54
+ placeholder="Ask anything here...",
55
+ container=True,
56
+ scale=7,
57
+ ),
58
  )
59
 
60
+ if __name__ == "__main__":
61
+ demo.launch()