Alhdrawi commited on
Commit
79e6dc7
·
verified ·
1 Parent(s): 33cc7dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -21
app.py CHANGED
@@ -2,13 +2,9 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
5
-
6
-
7
  API_TOKEN = os.getenv("HF_TOKEN")
8
-
9
  client = InferenceClient(token=API_TOKEN)
10
 
11
-
12
  def respond(
13
  message,
14
  history: list[tuple[str, str]],
@@ -17,31 +13,31 @@ def respond(
17
  temperature,
18
  top_p,
19
  ):
20
- messages = [{"role": "system", "content": system_message}]
21
-
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
-
28
- messages.append({"role": "user", "content": message})
29
 
30
  response = ""
31
 
32
- for message in client.chat_completion(
33
- model="Alhdrawi/alhdrawi",
34
- messages=messages,
35
- max_tokens=max_tokens,
36
- stream=True,
37
  temperature=temperature,
38
  top_p=top_p,
 
39
  ):
40
- token = message.choices[0].delta.content
41
- response += token
 
42
  yield response
43
 
44
-
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
 
2
  from huggingface_hub import InferenceClient
3
  import os
4
 
 
 
5
  API_TOKEN = os.getenv("HF_TOKEN")
 
6
  client = InferenceClient(token=API_TOKEN)
7
 
 
8
  def respond(
9
  message,
10
  history: list[tuple[str, str]],
 
13
  temperature,
14
  top_p,
15
  ):
16
+ # بناء نص المحادثة ك string واحد مع أدوار واضحة
17
+ conversation = f"System: {system_message}\n"
18
+ for user_msg, assistant_msg in history:
19
+ if user_msg:
20
+ conversation += f"User: {user_msg}\n"
21
+ if assistant_msg:
22
+ conversation += f"Assistant: {assistant_msg}\n"
23
+ conversation += f"User: {message}\nAssistant:"
 
24
 
25
  response = ""
26
 
27
+ # استدعاء text_generation مع التدفق (stream=True)
28
+ for output in client.text_generation(
29
+ model="Alhdrawi/alhdrawi",
30
+ inputs=conversation,
31
+ max_new_tokens=max_tokens,
32
  temperature=temperature,
33
  top_p=top_p,
34
+ stream=True,
35
  ):
36
+ # كل مرة يجي جزء جديد من النص
37
+ new_text = output.generated_text[len(response):]
38
+ response += new_text
39
  yield response
40
 
 
41
  demo = gr.ChatInterface(
42
  respond,
43
  additional_inputs=[