Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ def initialize_model():
|
|
44 |
return None, None
|
45 |
|
46 |
# Generate response
|
47 |
-
def generate_response(model, tokenizer, user_input, chat_history):
|
48 |
# Check if model and tokenizer are loaded
|
49 |
if model is None or tokenizer is None:
|
50 |
return "மாதிரி ஏற்றப்படவில்லை. 'மாதிரியை ஏற்று' பொத்தானைக் கிளிக் செய்யவும்." # Model not loaded, please click 'Load Model' button
|
@@ -73,15 +73,16 @@ def generate_response(model, tokenizer, user_input, chat_history):
|
|
73 |
# Tokenize input
|
74 |
inputs = tokenizer(prompt_template, return_tensors="pt", padding=True)
|
75 |
|
76 |
-
# Generate response
|
77 |
with torch.no_grad():
|
78 |
output = model.generate(
|
79 |
inputs["input_ids"],
|
80 |
attention_mask=inputs["attention_mask"],
|
81 |
max_new_tokens=256,
|
82 |
do_sample=True,
|
83 |
-
temperature=
|
84 |
-
top_p=
|
|
|
85 |
pad_token_id=tokenizer.eos_token_id,
|
86 |
eos_token_id=tokenizer.encode("<|im_end|>", add_special_tokens=False)[0] if "<|im_end|>" in tokenizer.get_vocab() else tokenizer.eos_token_id
|
87 |
)
|
@@ -104,24 +105,66 @@ def generate_response(model, tokenizer, user_input, chat_history):
|
|
104 |
print(f"Error generating response: {e}")
|
105 |
return f"பிழை ஏற்பட்டது. மீண்டும் முயற்சிக்கவும்." # Error occurred, please try again
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
# Create the Gradio interface
|
108 |
def create_chatbot_interface():
|
109 |
-
with gr.Blocks() as demo:
|
110 |
-
|
|
|
|
|
|
|
|
|
111 |
|
112 |
# Model loading indicator
|
113 |
with gr.Row():
|
114 |
model_status = gr.Markdown("⚠️ மாதிரி ஏற்றப்படவில்லை (Model not loaded)")
|
115 |
load_model_btn = gr.Button("மாதிரியை ஏற்று (Load Model)")
|
116 |
|
|
|
117 |
chatbot = gr.Chatbot(label="உரையாடல் (Conversation)", type="messages")
|
118 |
-
msg = gr.Textbox(label="உங்கள் செய்தி (Your Message)", placeholder="இங்கே தட்டச்சு செய்யவும்...")
|
119 |
-
clear = gr.Button("அழி (Clear)")
|
120 |
|
121 |
# Model and tokenizer states
|
122 |
model = gr.State(None)
|
123 |
tokenizer = gr.State(None)
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
# Function to load model on button click
|
126 |
def load_model_fn():
|
127 |
m, t = initialize_model()
|
@@ -129,41 +172,59 @@ def create_chatbot_interface():
|
|
129 |
return "✅ மாதிரி வெற்றிகரமாக ஏற்றப்பட்டது (Model loaded successfully)", m, t
|
130 |
else:
|
131 |
return "❌ மாதிரி ஏற்றுவதில் பிழை (Error loading model)", None, None
|
132 |
-
|
133 |
# Function to handle messages
|
134 |
-
def respond(message, chat_history, model_state, tokenizer_state):
|
135 |
# Check if model is loaded
|
136 |
if model_state is None:
|
137 |
bot_message = "மாதிரி ஏற்றப்படவில்லை. முதலில் 'மாதிரியை ஏற்று' பொத்தானைக் கிளிக் செய்யவும்."
|
138 |
else:
|
139 |
-
# Generate bot response
|
140 |
-
bot_message = generate_response(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# Append messages
|
143 |
chat_history.append({"role": "user", "content": message})
|
144 |
chat_history.append({"role": "assistant", "content": bot_message})
|
145 |
|
146 |
-
return "", chat_history
|
147 |
|
148 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
load_model_btn.click(
|
150 |
load_model_fn,
|
151 |
outputs=[model_status, model, tokenizer]
|
152 |
)
|
153 |
|
154 |
-
|
155 |
-
|
156 |
-
[msg, chatbot, model, tokenizer],
|
157 |
-
[msg, chatbot, model, tokenizer]
|
158 |
-
)
|
159 |
-
|
160 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
161 |
|
162 |
-
|
163 |
|
164 |
# Create and launch the demo
|
165 |
demo = create_chatbot_interface()
|
166 |
|
167 |
# Launch the demo
|
168 |
if __name__ == "__main__":
|
169 |
-
demo.launch()
|
|
|
44 |
return None, None
|
45 |
|
46 |
# Generate response
|
47 |
+
def generate_response(model, tokenizer, user_input, chat_history, temperature=0.2, top_p=1.0, top_k=40):
|
48 |
# Check if model and tokenizer are loaded
|
49 |
if model is None or tokenizer is None:
|
50 |
return "மாதிரி ஏற்றப்படவில்லை. 'மாதிரியை ஏற்று' பொத்தானைக் கிளிக் செய்யவும்." # Model not loaded, please click 'Load Model' button
|
|
|
73 |
# Tokenize input
|
74 |
inputs = tokenizer(prompt_template, return_tensors="pt", padding=True)
|
75 |
|
76 |
+
# Generate response with user-specified parameters
|
77 |
with torch.no_grad():
|
78 |
output = model.generate(
|
79 |
inputs["input_ids"],
|
80 |
attention_mask=inputs["attention_mask"],
|
81 |
max_new_tokens=256,
|
82 |
do_sample=True,
|
83 |
+
temperature=temperature,
|
84 |
+
top_p=top_p,
|
85 |
+
top_k=top_k,
|
86 |
pad_token_id=tokenizer.eos_token_id,
|
87 |
eos_token_id=tokenizer.encode("<|im_end|>", add_special_tokens=False)[0] if "<|im_end|>" in tokenizer.get_vocab() else tokenizer.eos_token_id
|
88 |
)
|
|
|
105 |
print(f"Error generating response: {e}")
|
106 |
return f"பிழை ஏற்பட்டது. மீண்டும் முயற்சிக்கவும்." # Error occurred, please try again
|
107 |
|
108 |
+
# Function to vote/like a response
|
109 |
+
def vote(data, vote_type, model_name):
|
110 |
+
# This is a placeholder for the voting functionality
|
111 |
+
# You can expand this to save votes to a file or database
|
112 |
+
print(f"Received {vote_type} for response: {data}")
|
113 |
+
return data
|
114 |
+
|
115 |
# Create the Gradio interface
|
116 |
def create_chatbot_interface():
|
117 |
+
with gr.Blocks(css="css/index.css") as demo:
|
118 |
+
title = "# தமிழ் உரையாடல் பொத்தான் (Tamil Chatbot)"
|
119 |
+
description = "Tamil LLaMA 7B Instruct model with user-controlled generation parameters."
|
120 |
+
|
121 |
+
gr.Markdown(title)
|
122 |
+
gr.Markdown(description)
|
123 |
|
124 |
# Model loading indicator
|
125 |
with gr.Row():
|
126 |
model_status = gr.Markdown("⚠️ மாதிரி ஏற்றப்படவில்லை (Model not loaded)")
|
127 |
load_model_btn = gr.Button("மாதிரியை ஏற்று (Load Model)")
|
128 |
|
129 |
+
# Define chatbot component to use with ChatInterface
|
130 |
chatbot = gr.Chatbot(label="உரையாடல் (Conversation)", type="messages")
|
|
|
|
|
131 |
|
132 |
# Model and tokenizer states
|
133 |
model = gr.State(None)
|
134 |
tokenizer = gr.State(None)
|
135 |
|
136 |
+
# Parameter sliders
|
137 |
+
with gr.Accordion("Generation Parameters", open=False):
|
138 |
+
temperature = gr.Slider(
|
139 |
+
label="temperature",
|
140 |
+
value=0.2,
|
141 |
+
minimum=0.0,
|
142 |
+
maximum=2.0,
|
143 |
+
step=0.05,
|
144 |
+
interactive=True,
|
145 |
+
info="Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic."
|
146 |
+
)
|
147 |
+
|
148 |
+
top_p = gr.Slider(
|
149 |
+
label="top_p",
|
150 |
+
value=1.0,
|
151 |
+
minimum=0.0,
|
152 |
+
maximum=1.0,
|
153 |
+
step=0.01,
|
154 |
+
interactive=True,
|
155 |
+
info="0.1 means only the tokens comprising the top 10% probability mass are considered. Suggest set to 1 and use temperature. 1 means 100% and will disable it"
|
156 |
+
)
|
157 |
+
|
158 |
+
top_k = gr.Slider(
|
159 |
+
label="top_k",
|
160 |
+
value=40,
|
161 |
+
minimum=0,
|
162 |
+
maximum=1000,
|
163 |
+
step=1,
|
164 |
+
interactive=True,
|
165 |
+
info="limits candidate tokens to a fixed number after sorting by probability. Setting it higher than the vocabulary size deactivates this limit."
|
166 |
+
)
|
167 |
+
|
168 |
# Function to load model on button click
|
169 |
def load_model_fn():
|
170 |
m, t = initialize_model()
|
|
|
172 |
return "✅ மாதிரி வெற்றிகரமாக ஏற்றப்பட்டது (Model loaded successfully)", m, t
|
173 |
else:
|
174 |
return "❌ மாதிரி ஏற்றுவதில் பிழை (Error loading model)", None, None
|
175 |
+
|
176 |
# Function to handle messages
|
177 |
+
def respond(message, chat_history, model_state, tokenizer_state, temp, tp, tk):
|
178 |
# Check if model is loaded
|
179 |
if model_state is None:
|
180 |
bot_message = "மாதிரி ஏற்றப்படவில்லை. முதலில் 'மாதிரியை ஏற்று' பொத்தானைக் கிளிக் செய்யவும்."
|
181 |
else:
|
182 |
+
# Generate bot response with parameters
|
183 |
+
bot_message = generate_response(
|
184 |
+
model_state,
|
185 |
+
tokenizer_state,
|
186 |
+
message,
|
187 |
+
chat_history,
|
188 |
+
temperature=temp,
|
189 |
+
top_p=tp,
|
190 |
+
top_k=tk
|
191 |
+
)
|
192 |
|
193 |
# Append messages
|
194 |
chat_history.append({"role": "user", "content": message})
|
195 |
chat_history.append({"role": "assistant", "content": bot_message})
|
196 |
|
197 |
+
return "", chat_history
|
198 |
|
199 |
+
# Chat interface
|
200 |
+
chat_interface = gr.ChatInterface(
|
201 |
+
fn=lambda message, history, m=model, t=tokenizer, temp=temperature, tp=top_p, tk=top_k:
|
202 |
+
respond(message, history, m.value, t.value, temp, tp, tk)[1],
|
203 |
+
chatbot=chatbot,
|
204 |
+
examples=[
|
205 |
+
["வணக்கம், நீங்கள் யார்?"],
|
206 |
+
["நான் பெரிய பணக்காரன் இல்லை, லேட்டஸ்ட் iPhone-இல் நிறைய பணம் செலவழிக்க வேண்டுமா?"],
|
207 |
+
["பட்டியலை வரிசைப்படுத்த பைதான் செயல்பாட்டை எழுதவும்."],
|
208 |
+
["சிவப்பும் மஞ்சளும் கலந்தால் என்ன நிறமாக இருக்கும்?"],
|
209 |
+
["விரைவாக தூங்குவது எப்படி?"]
|
210 |
+
],
|
211 |
+
additional_inputs=[temperature, top_p, top_k]
|
212 |
+
)
|
213 |
+
|
214 |
+
# Connect the model loading button
|
215 |
load_model_btn.click(
|
216 |
load_model_fn,
|
217 |
outputs=[model_status, model, tokenizer]
|
218 |
)
|
219 |
|
220 |
+
# Add like button functionality
|
221 |
+
chatbot.like(vote, None, None)
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
+
return demo
|
224 |
|
225 |
# Create and launch the demo
|
226 |
demo = create_chatbot_interface()
|
227 |
|
228 |
# Launch the demo
|
229 |
if __name__ == "__main__":
|
230 |
+
demo.queue(max_size=3).launch()
|