Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
# Define device
|
6 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
-
|
8 |
-
# Load the model and tokenizer
|
9 |
model_name = "hosseinhimself/ISANG-v1.0-8B"
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
""
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
history = []
|
59 |
-
|
60 |
-
def interface_function(user_input):
|
61 |
-
global history
|
62 |
-
history = chat_with_model(history, user_input)
|
63 |
-
return gradio_format(history)
|
64 |
-
|
65 |
-
# Create Gradio interface
|
66 |
-
chatbot = gr.ChatInterface(
|
67 |
-
fn=interface_function,
|
68 |
-
inputs=[gr.Textbox(lines=2, label="Your Input")],
|
69 |
-
outputs=[gr.Chatbot(label="Chat History")],
|
70 |
-
title="Persian Chatbot",
|
71 |
-
description="A chatbot that translates or responds to Persian prompts using ISANG-v1.0-8B model."
|
72 |
)
|
73 |
|
74 |
-
# Launch the app
|
75 |
if __name__ == "__main__":
|
76 |
-
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
from peft import PeftModel, PeftConfig
|
5 |
+
import spaces
|
6 |
+
import time
|
7 |
|
|
|
|
|
|
|
|
|
8 |
model_name = "hosseinhimself/ISANG-v1.0-8B"
|
9 |
+
base_model_name = "unsloth/Meta-Llama-3.1-8B"
|
10 |
+
|
11 |
+
# Load tokenizer globally
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
+
|
14 |
+
@spaces.GPU
|
15 |
+
def load_model():
|
16 |
+
try:
|
17 |
+
# Load the base model
|
18 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
19 |
+
base_model_name,
|
20 |
+
torch_dtype=torch.float16,
|
21 |
+
device_map="auto",
|
22 |
+
trust_remote_code=True,
|
23 |
+
low_cpu_mem_usage=True
|
24 |
+
)
|
25 |
+
# Load the PEFT model
|
26 |
+
model = PeftModel.from_pretrained(base_model, model_name)
|
27 |
+
print(f"Model loaded successfully. Using device: {model.device}")
|
28 |
+
return model
|
29 |
+
except Exception as e:
|
30 |
+
print(f"Error loading model: {e}")
|
31 |
+
raise
|
32 |
+
|
33 |
+
@spaces.GPU
|
34 |
+
def generate_text(prompt):
|
35 |
+
model = load_model()
|
36 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
37 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
38 |
+
with torch.no_grad():
|
39 |
+
outputs = model.generate(**inputs, max_new_tokens=200, num_return_sequences=1, temperature=0.7)
|
40 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
return response
|
42 |
+
|
43 |
+
gradio_app = gr.Interface(
|
44 |
+
generate_text,
|
45 |
+
inputs=gr.Textbox(label="Enter your message", lines=3),
|
46 |
+
outputs=gr.Textbox(label="Chatbot Response"),
|
47 |
+
title="ISANG Chatbot",
|
48 |
+
description=f"""This is a simple chatbot powered by the ISANG model. It is fine-tuned from the {base_model_name} model.
|
49 |
+
Enter your message and see how the chatbot responds!""",
|
50 |
+
examples=[
|
51 |
+
["سلام، چطوری؟"],
|
52 |
+
["برام یه داستان تعریف کن"],
|
53 |
+
["بهترین کتابی که خوندی چی بوده؟"],
|
54 |
+
["توی اوقات فراغتت چی کار میکنی؟"],
|
55 |
+
["نظرت درباره هوش مصنوعی چیه؟"]
|
56 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
)
|
58 |
|
|
|
59 |
if __name__ == "__main__":
|
60 |
+
gradio_app.launch()
|