Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -139,23 +139,18 @@ def generate_response(user_input: str, temperature: float = 0.7, max_tokens: int
|
|
| 139 |
return f"Error: {str(e)}"
|
| 140 |
|
| 141 |
|
| 142 |
-
def
|
| 143 |
-
"""Chat function
|
| 144 |
-
if not
|
| 145 |
-
return "",
|
| 146 |
|
| 147 |
-
|
| 148 |
|
| 149 |
-
if
|
| 150 |
-
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
{"role": "user", "content": user_input},
|
| 155 |
-
{"role": "assistant", "content": response}
|
| 156 |
-
]
|
| 157 |
-
|
| 158 |
-
return "", history
|
| 159 |
|
| 160 |
|
| 161 |
def feedback_good():
|
|
@@ -168,14 +163,14 @@ def feedback_good():
|
|
| 168 |
def feedback_bad():
|
| 169 |
if current_conv_id > 0:
|
| 170 |
db.update_feedback(current_conv_id, -1)
|
| 171 |
-
return "π Thanks
|
| 172 |
return "No conversation to rate yet."
|
| 173 |
|
| 174 |
|
| 175 |
-
def
|
| 176 |
global conversation_history
|
| 177 |
conversation_history = []
|
| 178 |
-
return [], "
|
| 179 |
|
| 180 |
|
| 181 |
def retrain(epochs):
|
|
@@ -185,7 +180,7 @@ def retrain(epochs):
|
|
| 185 |
good_convs = db.get_good_conversations()
|
| 186 |
|
| 187 |
if not good_convs:
|
| 188 |
-
return "No approved conversations yet. Rate some responses first!"
|
| 189 |
|
| 190 |
extra_data = ""
|
| 191 |
for conv in good_convs:
|
|
@@ -199,7 +194,7 @@ def retrain(epochs):
|
|
| 199 |
tokenizer = trainer.tokenizer
|
| 200 |
|
| 201 |
loss = history.history['loss'][-1]
|
| 202 |
-
return f"β
Training
|
| 203 |
|
| 204 |
|
| 205 |
def get_stats():
|
|
@@ -208,9 +203,9 @@ def get_stats():
|
|
| 208 |
|
| 209 |
| Metric | Count |
|
| 210 |
|--------|-------|
|
| 211 |
-
| π¬ Conversations | {stats['total']} |
|
| 212 |
-
| π Positive | {stats['positive']} |
|
| 213 |
-
| π Negative | {stats['negative']} |
|
| 214 |
"""
|
| 215 |
|
| 216 |
|
|
@@ -220,23 +215,22 @@ initialize()
|
|
| 220 |
print("Initialization complete!")
|
| 221 |
|
| 222 |
|
| 223 |
-
# Create Gradio interface
|
| 224 |
with gr.Blocks(title="Veda Programming Assistant") as demo:
|
| 225 |
|
| 226 |
gr.Markdown("""
|
| 227 |
# ποΈ Veda Programming Assistant
|
| 228 |
|
| 229 |
-
I can chat
|
| 230 |
""")
|
| 231 |
|
| 232 |
with gr.Tabs():
|
| 233 |
|
| 234 |
with gr.TabItem("π¬ Chat"):
|
| 235 |
-
# Chatbot
|
| 236 |
chatbot = gr.Chatbot(
|
| 237 |
label="Conversation",
|
| 238 |
-
height=400
|
| 239 |
-
type="messages"
|
| 240 |
)
|
| 241 |
|
| 242 |
with gr.Row():
|
|
@@ -273,22 +267,22 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
|
|
| 273 |
|
| 274 |
# Event handlers
|
| 275 |
send_btn.click(
|
| 276 |
-
|
| 277 |
inputs=[msg, chatbot, temperature, max_tokens],
|
| 278 |
outputs=[msg, chatbot]
|
| 279 |
)
|
| 280 |
|
| 281 |
msg.submit(
|
| 282 |
-
|
| 283 |
inputs=[msg, chatbot, temperature, max_tokens],
|
| 284 |
outputs=[msg, chatbot]
|
| 285 |
)
|
| 286 |
|
| 287 |
good_btn.click(feedback_good, outputs=feedback_msg)
|
| 288 |
bad_btn.click(feedback_bad, outputs=feedback_msg)
|
| 289 |
-
clear_btn.click(
|
| 290 |
|
| 291 |
-
gr.Markdown("### π‘
|
| 292 |
gr.Examples(
|
| 293 |
examples=[
|
| 294 |
["Hello! What can you do?"],
|
|
@@ -302,15 +296,20 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
|
|
| 302 |
)
|
| 303 |
|
| 304 |
with gr.TabItem("π Training"):
|
| 305 |
-
gr.Markdown("
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
train_epochs = gr.Slider(
|
| 309 |
minimum=5,
|
| 310 |
maximum=20,
|
| 311 |
value=10,
|
| 312 |
step=1,
|
| 313 |
-
label="Epochs"
|
| 314 |
)
|
| 315 |
train_btn = gr.Button("π Retrain Model", variant="primary")
|
| 316 |
train_output = gr.Markdown()
|
|
@@ -318,20 +317,15 @@ with gr.Blocks(title="Veda Programming Assistant") as demo:
|
|
| 318 |
|
| 319 |
with gr.TabItem("π Statistics"):
|
| 320 |
stats_out = gr.Markdown()
|
| 321 |
-
refresh_btn = gr.Button("π Refresh")
|
| 322 |
refresh_btn.click(get_stats, outputs=stats_out)
|
| 323 |
-
|
| 324 |
-
gr.Markdown("""
|
| 325 |
-
### How it works
|
| 326 |
-
1. Chat with the assistant
|
| 327 |
-
2. Rate responses as Good π or Bad π
|
| 328 |
-
3. Good responses are saved for training
|
| 329 |
-
4. Click 'Retrain Model' to improve the assistant
|
| 330 |
-
""")
|
| 331 |
|
| 332 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
|
| 335 |
-
# Launch
|
| 336 |
if __name__ == "__main__":
|
| 337 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 139 |
return f"Error: {str(e)}"
|
| 140 |
|
| 141 |
|
| 142 |
+
def respond(message, chat_history, temperature, max_tokens):
|
| 143 |
+
"""Chat function - returns tuple format"""
|
| 144 |
+
if not message or not message.strip():
|
| 145 |
+
return "", chat_history if chat_history else []
|
| 146 |
|
| 147 |
+
bot_message = generate_response(message, temperature, max_tokens)
|
| 148 |
|
| 149 |
+
if chat_history is None:
|
| 150 |
+
chat_history = []
|
| 151 |
|
| 152 |
+
chat_history.append((message, bot_message))
|
| 153 |
+
return "", chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
def feedback_good():
|
|
|
|
| 163 |
def feedback_bad():
|
| 164 |
if current_conv_id > 0:
|
| 165 |
db.update_feedback(current_conv_id, -1)
|
| 166 |
+
return "π Thanks! I'll try to improve."
|
| 167 |
return "No conversation to rate yet."
|
| 168 |
|
| 169 |
|
| 170 |
+
def clear_chat():
|
| 171 |
global conversation_history
|
| 172 |
conversation_history = []
|
| 173 |
+
return [], "Chat cleared."
|
| 174 |
|
| 175 |
|
| 176 |
def retrain(epochs):
|
|
|
|
| 180 |
good_convs = db.get_good_conversations()
|
| 181 |
|
| 182 |
if not good_convs:
|
| 183 |
+
return "No approved conversations yet. Rate some responses as 'Good' first!"
|
| 184 |
|
| 185 |
extra_data = ""
|
| 186 |
for conv in good_convs:
|
|
|
|
| 194 |
tokenizer = trainer.tokenizer
|
| 195 |
|
| 196 |
loss = history.history['loss'][-1]
|
| 197 |
+
return f"β
Training complete! Loss: {loss:.4f}, Used {len(good_convs)} conversations"
|
| 198 |
|
| 199 |
|
| 200 |
def get_stats():
|
|
|
|
| 203 |
|
| 204 |
| Metric | Count |
|
| 205 |
|--------|-------|
|
| 206 |
+
| π¬ Total Conversations | {stats['total']} |
|
| 207 |
+
| π Positive Feedback | {stats['positive']} |
|
| 208 |
+
| π Negative Feedback | {stats['negative']} |
|
| 209 |
"""
|
| 210 |
|
| 211 |
|
|
|
|
| 215 |
print("Initialization complete!")
|
| 216 |
|
| 217 |
|
| 218 |
+
# Create Gradio interface - NO type parameter
|
| 219 |
with gr.Blocks(title="Veda Programming Assistant") as demo:
|
| 220 |
|
| 221 |
gr.Markdown("""
|
| 222 |
# ποΈ Veda Programming Assistant
|
| 223 |
|
| 224 |
+
I can **chat**, **write code**, **explain concepts**, and **answer questions**!
|
| 225 |
""")
|
| 226 |
|
| 227 |
with gr.Tabs():
|
| 228 |
|
| 229 |
with gr.TabItem("π¬ Chat"):
|
| 230 |
+
# Simple Chatbot without type parameter
|
| 231 |
chatbot = gr.Chatbot(
|
| 232 |
label="Conversation",
|
| 233 |
+
height=400
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
with gr.Row():
|
|
|
|
| 267 |
|
| 268 |
# Event handlers
|
| 269 |
send_btn.click(
|
| 270 |
+
respond,
|
| 271 |
inputs=[msg, chatbot, temperature, max_tokens],
|
| 272 |
outputs=[msg, chatbot]
|
| 273 |
)
|
| 274 |
|
| 275 |
msg.submit(
|
| 276 |
+
respond,
|
| 277 |
inputs=[msg, chatbot, temperature, max_tokens],
|
| 278 |
outputs=[msg, chatbot]
|
| 279 |
)
|
| 280 |
|
| 281 |
good_btn.click(feedback_good, outputs=feedback_msg)
|
| 282 |
bad_btn.click(feedback_bad, outputs=feedback_msg)
|
| 283 |
+
clear_btn.click(clear_chat, outputs=[chatbot, feedback_msg])
|
| 284 |
|
| 285 |
+
gr.Markdown("### π‘ Try these examples:")
|
| 286 |
gr.Examples(
|
| 287 |
examples=[
|
| 288 |
["Hello! What can you do?"],
|
|
|
|
| 296 |
)
|
| 297 |
|
| 298 |
with gr.TabItem("π Training"):
|
| 299 |
+
gr.Markdown("""
|
| 300 |
+
### Improve the Assistant
|
| 301 |
+
|
| 302 |
+
1. Chat with the assistant
|
| 303 |
+
2. Rate good responses with π
|
| 304 |
+
3. Click "Retrain Model" to learn from good conversations
|
| 305 |
+
""")
|
| 306 |
|
| 307 |
train_epochs = gr.Slider(
|
| 308 |
minimum=5,
|
| 309 |
maximum=20,
|
| 310 |
value=10,
|
| 311 |
step=1,
|
| 312 |
+
label="Training Epochs"
|
| 313 |
)
|
| 314 |
train_btn = gr.Button("π Retrain Model", variant="primary")
|
| 315 |
train_output = gr.Markdown()
|
|
|
|
| 317 |
|
| 318 |
with gr.TabItem("π Statistics"):
|
| 319 |
stats_out = gr.Markdown()
|
| 320 |
+
refresh_btn = gr.Button("π Refresh Statistics")
|
| 321 |
refresh_btn.click(get_stats, outputs=stats_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
+
gr.Markdown("""
|
| 324 |
+
---
|
| 325 |
+
**Veda Programming Assistant** - Learning from every conversation!
|
| 326 |
+
""")
|
| 327 |
|
| 328 |
|
| 329 |
+
# Launch the app
|
| 330 |
if __name__ == "__main__":
|
| 331 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|