Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,40 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
raise ValueError("Please set the HF_API_TOKEN environment variable with your Hugging Face API token.")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def respond(
|
| 14 |
-
message,
|
| 15 |
-
history: list[tuple[str, str]],
|
| 16 |
-
system_message,
|
| 17 |
-
max_tokens,
|
| 18 |
-
temperature,
|
| 19 |
-
top_p,
|
| 20 |
-
):
|
| 21 |
-
messages = [{"role": "system", "content": system_message}]
|
| 22 |
-
|
| 23 |
-
for val in history:
|
| 24 |
-
if val[0]:
|
| 25 |
-
messages.append({"role": "user", "content": val[0]})
|
| 26 |
-
if val[1]:
|
| 27 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
-
|
| 29 |
-
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
response = ""
|
| 32 |
-
|
| 33 |
-
for
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
temperature=temperature,
|
| 38 |
top_p=top_p,
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
yield response
|
| 43 |
|
| 44 |
demo = gr.ChatInterface(
|
| 45 |
-
respond,
|
| 46 |
additional_inputs=[
|
| 47 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 48 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
@@ -55,6 +47,7 @@ demo = gr.ChatInterface(
|
|
| 55 |
label="Top-p (nucleus sampling)",
|
| 56 |
),
|
| 57 |
],
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# لاحظ: يجب تمرير التوكن بطريقة أخرى، مثلاً عبر متغير بيئة أو إعدادات المساحة
|
| 5 |
+
# هنا ننشئ العميل بدون توكن (إذا النموذج يسمح)
|
| 6 |
+
client = InferenceClient("mradermacher/airoboros_none_resp_gpt-4o-mini_inst_gpt-4o_resp-GGUF")
|
|
|
|
| 7 |
|
| 8 |
+
def respond(messages, system_message, max_tokens, temperature, top_p):
|
| 9 |
+
chat_messages = [{"role": "system", "content": system_message}] + messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
response = ""
|
| 12 |
+
prompt = ""
|
| 13 |
+
for msg in chat_messages:
|
| 14 |
+
role = msg.get("role", "")
|
| 15 |
+
content = msg.get("content", "")
|
| 16 |
+
if role == "system":
|
| 17 |
+
prompt += f"System: {content}\n"
|
| 18 |
+
elif role == "user":
|
| 19 |
+
prompt += f"User: {content}\n"
|
| 20 |
+
elif role == "assistant":
|
| 21 |
+
prompt += f"Assistant: {content}\n"
|
| 22 |
+
prompt += "Assistant: "
|
| 23 |
+
|
| 24 |
+
generation = client.text_generation(
|
| 25 |
+
prompt,
|
| 26 |
+
max_new_tokens=max_tokens,
|
| 27 |
temperature=temperature,
|
| 28 |
top_p=top_p,
|
| 29 |
+
stream=True,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
for token in generation:
|
| 33 |
+
response += token.text
|
| 34 |
yield response
|
| 35 |
|
| 36 |
demo = gr.ChatInterface(
|
| 37 |
+
fn=respond,
|
| 38 |
additional_inputs=[
|
| 39 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 40 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 47 |
label="Top-p (nucleus sampling)",
|
| 48 |
),
|
| 49 |
],
|
| 50 |
+
type="messages",
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|