Spaces:
Runtime error
Runtime error
add falcon-7b to space
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -15,8 +17,10 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
|
@@ -25,23 +29,19 @@ def respond(
|
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
-
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
|
@@ -59,6 +59,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
|
| 4 |
+
# بارگذاری مدل و توکنایزر Falcon-7B-Instruct
|
| 5 |
+
model_name = "tiiuae/falcon-7b-instruct"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
|
| 9 |
+
# ایجاد pipeline برای متنسازی
|
| 10 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 11 |
|
| 12 |
def respond(
|
| 13 |
message,
|
|
|
|
| 17 |
temperature,
|
| 18 |
top_p,
|
| 19 |
):
|
| 20 |
+
# ساخت پیامها برای مدل
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
| 23 |
+
# اضافه کردن تاریخچه چت
|
| 24 |
for val in history:
|
| 25 |
if val[0]:
|
| 26 |
messages.append({"role": "user", "content": val[0]})
|
|
|
|
| 29 |
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
+
# تولید پاسخ با استفاده از مدل
|
| 33 |
+
response = pipe(
|
|
|
|
| 34 |
messages,
|
| 35 |
+
max_new_tokens=max_tokens,
|
|
|
|
| 36 |
temperature=temperature,
|
| 37 |
top_p=top_p,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
yield response
|
| 42 |
|
| 43 |
"""
|
| 44 |
+
برای اطلاعات بیشتر در مورد تنظیمات ChatInterface، به مستندات Gradio مراجعه کنید: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
|
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|