Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
21 |
-
|
22 |
-
for
|
23 |
-
if
|
24 |
-
|
25 |
-
if
|
26 |
-
|
27 |
-
|
28 |
-
messages.append({"role": "user", "content": message})
|
29 |
|
30 |
response = ""
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
temperature=temperature,
|
38 |
top_p=top_p,
|
|
|
39 |
):
|
40 |
-
|
41 |
-
|
|
|
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=[
|