Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,69 +2,75 @@ import gradio as gr
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
device = torch.device("
|
7 |
|
8 |
-
# Load model and tokenizer
|
9 |
-
model_name = "hosseinhimself/ISANG-v1.0-8B"
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
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 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
|
69 |
-
# Launch the
|
70 |
-
|
|
|
|
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 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
12 |
|
13 |
+
def chat_with_model(history, user_input):
|
14 |
+
"""
|
15 |
+
Generate a response using the model, considering the last two interactions.
|
16 |
+
|
17 |
+
Parameters:
|
18 |
+
history (list of tuples): Conversation history as a list of (user, bot) pairs.
|
19 |
+
user_input (str): The latest user input.
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
history (list of tuples): Updated conversation history.
|
23 |
+
"""
|
24 |
+
# Use the last two interactions for context
|
25 |
+
context = ""
|
26 |
+
for user_message, bot_message in history[-2:]:
|
27 |
+
context += f"User: {user_message}\nBot: {bot_message}\n"
|
28 |
+
|
29 |
+
# Add the current user input
|
30 |
+
context += f"User: {user_input}\nBot:"
|
31 |
+
|
32 |
+
# Tokenize and generate a response
|
33 |
+
inputs = tokenizer(context, return_tensors="pt", truncation=True).to(device)
|
34 |
+
output = model.generate(inputs.input_ids, max_new_tokens=100)
|
35 |
+
bot_response = tokenizer.decode(output[0], skip_special_tokens=True)
|
36 |
+
|
37 |
+
# Extract only the bot's new response (to avoid repeating context)
|
38 |
+
bot_response = bot_response[len(context):].strip()
|
39 |
+
|
40 |
+
# Update the conversation history
|
41 |
+
history.append((user_input, bot_response))
|
42 |
+
|
43 |
+
return history
|
44 |
+
|
45 |
+
def gradio_format(history):
|
46 |
+
"""
|
47 |
+
Format the history for Gradio ChatInterface.
|
48 |
+
|
49 |
+
Parameters:
|
50 |
+
history (list of tuples): Conversation history as a list of (user, bot) pairs.
|
51 |
+
|
52 |
+
Returns:
|
53 |
+
List of dictionaries compatible with Gradio ChatInterface.
|
54 |
+
"""
|
55 |
+
return [[user, bot] for user, bot in history]
|
56 |
+
|
57 |
+
# Initialize empty history
|
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 |
+
chatbot.launch()
|