Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,43 @@
|
|
| 1 |
-
import spaces
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
-
|
| 6 |
-
from transformers import (
|
| 7 |
-
AutoTokenizer,
|
| 8 |
-
AutoModelForCausalLM
|
| 9 |
-
)
|
| 10 |
|
| 11 |
|
| 12 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 13 |
|
| 14 |
|
| 15 |
print("Loading tokenizer...")
|
| 16 |
-
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 18 |
-
MODEL_ID
|
| 19 |
-
)
|
| 20 |
|
| 21 |
|
| 22 |
print("Loading model...")
|
| 23 |
|
| 24 |
-
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
MODEL_ID,
|
| 27 |
device_map="auto",
|
| 28 |
-
|
|
|
|
| 29 |
)
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
print("Model loaded!")
|
| 33 |
|
| 34 |
|
| 35 |
def chat(message, history):
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
for item in history:
|
| 40 |
-
if isinstance(item, dict):
|
| 41 |
-
messages.append(item)
|
| 42 |
-
else:
|
| 43 |
-
user_msg, bot_msg = item
|
| 44 |
-
|
| 45 |
-
messages.append(
|
| 46 |
-
{
|
| 47 |
-
"role": "user",
|
| 48 |
-
"content": user_msg
|
| 49 |
-
}
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
messages.append(
|
| 53 |
-
{
|
| 54 |
-
"role": "assistant",
|
| 55 |
-
"content": bot_msg
|
| 56 |
-
}
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
messages.append(
|
| 61 |
-
{
|
| 62 |
-
"role": "user",
|
| 63 |
-
"content": message
|
| 64 |
-
}
|
| 65 |
-
)
|
| 66 |
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
prompt =
|
| 69 |
-
messages,
|
| 70 |
-
tokenize=False,
|
| 71 |
-
add_generation_prompt=True
|
| 72 |
-
)
|
| 73 |
|
| 74 |
|
| 75 |
inputs = tokenizer(
|
| 76 |
prompt,
|
| 77 |
return_tensors="pt"
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
if torch.cuda.is_available():
|
| 82 |
-
inputs = inputs.to("cuda")
|
| 83 |
|
| 84 |
|
| 85 |
with torch.no_grad():
|
|
@@ -94,28 +52,21 @@ def chat(message, history):
|
|
| 94 |
|
| 95 |
|
| 96 |
result = tokenizer.decode(
|
| 97 |
-
output[0]
|
| 98 |
skip_special_tokens=True
|
| 99 |
)
|
| 100 |
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
demo = gr.ChatInterface(
|
| 107 |
fn=chat,
|
| 108 |
-
title="Rezaeian
|
| 109 |
-
description=""
|
| 110 |
-
Rezaeian-StatsAI
|
| 111 |
-
|
| 112 |
-
Created by Amirhossein Rezaeian.
|
| 113 |
-
|
| 114 |
-
A Persian statistical AI model fine-tuned on University of Tehran statistics notes.
|
| 115 |
-
|
| 116 |
-
Base model:
|
| 117 |
-
Qwen2.5-7B-Instruct
|
| 118 |
-
"""
|
| 119 |
)
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 7 |
|
| 8 |
|
| 9 |
print("Loading tokenizer...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
print("Loading model...")
|
| 14 |
|
|
|
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
MODEL_ID,
|
| 17 |
device_map="auto",
|
| 18 |
+
dtype=torch.float16,
|
| 19 |
+
trust_remote_code=True
|
| 20 |
)
|
| 21 |
|
| 22 |
+
model.eval()
|
| 23 |
|
| 24 |
print("Model loaded!")
|
| 25 |
|
| 26 |
|
| 27 |
def chat(message, history):
|
| 28 |
|
| 29 |
+
prompt = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
for user, bot in history:
|
| 32 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 33 |
|
| 34 |
+
prompt += f"User: {message}\nAssistant:"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
inputs = tokenizer(
|
| 38 |
prompt,
|
| 39 |
return_tensors="pt"
|
| 40 |
+
).to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
with torch.no_grad():
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
result = tokenizer.decode(
|
| 55 |
+
output[0],
|
| 56 |
skip_special_tokens=True
|
| 57 |
)
|
| 58 |
|
| 59 |
|
| 60 |
+
answer = result.split("Assistant:")[-1]
|
| 61 |
+
|
| 62 |
+
return answer
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
demo = gr.ChatInterface(
|
| 67 |
fn=chat,
|
| 68 |
+
title="Rezaeian StatsAI",
|
| 69 |
+
description="AI Assistant"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
|