Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
# بارگذاری مدل
|
| 5 |
-
model_name = "
|
| 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)
|
|
@@ -22,4 +22,43 @@ def respond(
|
|
| 22 |
# ساخت پیامها برای مدل
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
|
| 25 |
-
# اضافه
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
|
| 4 |
+
# بارگذاری مدل GPT-J-6B
|
| 5 |
+
model_name = "EleutherAI/gpt-j-6b"
|
| 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)
|
|
|
|
| 22 |
# ساخت پیامها برای مدل
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
| 24 |
|
| 25 |
+
# اضافه کردن تاریخچه چت
|
| 26 |
+
for val in history:
|
| 27 |
+
if val[0]:
|
| 28 |
+
messages.append({"role": "user", "content": val[0]})
|
| 29 |
+
if val[1]:
|
| 30 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 31 |
+
|
| 32 |
+
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
# تولید پاسخ با استفاده از مدل
|
| 35 |
+
response = pipe(
|
| 36 |
+
messages,
|
| 37 |
+
max_new_tokens=max_tokens,
|
| 38 |
+
temperature=temperature,
|
| 39 |
+
top_p=top_p,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
)[0]["generated_text"]
|
| 42 |
+
|
| 43 |
+
yield response
|
| 44 |
+
|
| 45 |
+
# ایجاد رابط کاربری با Gradio
|
| 46 |
+
demo = gr.ChatInterface(
|
| 47 |
+
respond,
|
| 48 |
+
additional_inputs=[
|
| 49 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
+
gr.Slider(
|
| 53 |
+
minimum=0.1,
|
| 54 |
+
maximum=1.0,
|
| 55 |
+
value=0.95,
|
| 56 |
+
step=0.05,
|
| 57 |
+
label="Top-p (nucleus sampling)",
|
| 58 |
+
),
|
| 59 |
+
],
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# اجرای برنامه
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|