Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# بارگذاری توکنایزر و مدل
|
| 8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
-
|
| 11 |
-
# ایجاد pipeline برای متنسازی
|
| 12 |
-
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
|
@@ -20,27 +13,26 @@ def respond(
|
|
| 20 |
top_p,
|
| 21 |
):
|
| 22 |
# ساخت پیامها برای مدل
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
for val in history:
|
| 25 |
if val[0]:
|
| 26 |
-
|
| 27 |
if val[1]:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
# تولید پاسخ با استفاده از مدل
|
| 32 |
-
response =
|
| 33 |
-
|
| 34 |
-
|
| 35 |
temperature=temperature,
|
| 36 |
top_p=top_p,
|
| 37 |
-
|
| 38 |
-
)[0]["generated_text"]
|
| 39 |
-
|
| 40 |
-
# حذف prompt از پاسخ
|
| 41 |
-
response = response[len(prompt):].strip()
|
| 42 |
|
| 43 |
-
yield response
|
| 44 |
|
| 45 |
# ایجاد رابط کاربری با Gradio
|
| 46 |
demo = gr.ChatInterface(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# استفاده از Hugging Face Inference API برای GPT-3.5-Turbo
|
| 5 |
+
client = InferenceClient("openai/gpt-3.5-turbo")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(
|
| 8 |
message,
|
|
|
|
| 13 |
top_p,
|
| 14 |
):
|
| 15 |
# ساخت پیامها برای مدل
|
| 16 |
+
messages = [{"role": "system", "content": system_message}]
|
| 17 |
+
|
| 18 |
+
# اضافه کردن تاریخچه چت
|
| 19 |
for val in history:
|
| 20 |
if val[0]:
|
| 21 |
+
messages.append({"role": "user", "content": val[0]})
|
| 22 |
if val[1]:
|
| 23 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 24 |
+
|
| 25 |
+
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
# تولید پاسخ با استفاده از مدل
|
| 28 |
+
response = client.chat_completion(
|
| 29 |
+
messages,
|
| 30 |
+
max_tokens=max_tokens,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
yield response["choices"][0]["message"]["content"]
|
| 36 |
|
| 37 |
# ایجاد رابط کاربری با Gradio
|
| 38 |
demo = gr.ChatInterface(
|