Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from transformers import (
|
| 5 |
+
AutoTokenizer,
|
| 6 |
+
AutoModelForCausalLM
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
print("Loading tokenizer...")
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
print("Loading model...")
|
| 18 |
+
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
MODEL_ID,
|
| 21 |
+
torch_dtype=torch.float16,
|
| 22 |
+
device_map="auto"
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
model.eval()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
system_prompt = """
|
| 30 |
+
You are Rezaeian-StatsAI.
|
| 31 |
+
You are a university statistics and probability assistant.
|
| 32 |
+
Answer in Persian.
|
| 33 |
+
Solve engineering statistics problems step by step.
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def chat(question):
|
| 38 |
+
|
| 39 |
+
prompt = f"""
|
| 40 |
+
<|im_start|>system
|
| 41 |
+
{system_prompt}
|
| 42 |
+
<|im_end|>
|
| 43 |
+
|
| 44 |
+
<|im_start|>user
|
| 45 |
+
{question}
|
| 46 |
+
<|im_end|>
|
| 47 |
+
|
| 48 |
+
<|im_start|>assistant
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
inputs = tokenizer(
|
| 53 |
+
prompt,
|
| 54 |
+
return_tensors="pt"
|
| 55 |
+
).to(model.device)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
|
| 60 |
+
output = model.generate(
|
| 61 |
+
**inputs,
|
| 62 |
+
max_new_tokens=512,
|
| 63 |
+
temperature=0.7,
|
| 64 |
+
do_sample=True
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
answer = tokenizer.decode(
|
| 69 |
+
output[0],
|
| 70 |
+
skip_special_tokens=True
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
return answer.split("assistant")[-1]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
demo = gr.Interface(
|
| 78 |
+
fn=chat,
|
| 79 |
+
inputs=gr.Textbox(
|
| 80 |
+
label="سوال آماری"
|
| 81 |
+
),
|
| 82 |
+
outputs=gr.Textbox(
|
| 83 |
+
label="پاسخ مدل"
|
| 84 |
+
),
|
| 85 |
+
title="Rezaeian-StatsAI",
|
| 86 |
+
description="AI assistant for Engineering Statistics"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
demo.launch()
|