Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
"""Gradio App for Veda Programming Assistant - Gradio 6.2.0"""
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
|
@@ -139,13 +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 for Gradio"""
|
| 144 |
-
if not user_input.strip():
|
| 145 |
-
return "", history
|
| 146 |
|
| 147 |
response = generate_response(user_input, temperature, max_tokens)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
history = history + [[user_input, response]]
|
|
|
|
| 149 |
return "", history
|
| 150 |
|
| 151 |
|
|
@@ -163,7 +168,7 @@ def feedback_bad():
|
|
| 163 |
return "No conversation to rate yet."
|
| 164 |
|
| 165 |
|
| 166 |
-
def
|
| 167 |
global conversation_history
|
| 168 |
conversation_history = []
|
| 169 |
return [], "Conversation cleared."
|
|
@@ -205,28 +210,27 @@ def get_stats():
|
|
| 205 |
"""
|
| 206 |
|
| 207 |
|
| 208 |
-
# Initialize model
|
| 209 |
print("Starting initialization...")
|
| 210 |
initialize()
|
| 211 |
print("Initialization complete!")
|
| 212 |
|
| 213 |
|
| 214 |
-
# Create Gradio interface
|
| 215 |
-
with gr.Blocks(title="Veda Programming Assistant"
|
| 216 |
|
| 217 |
gr.Markdown("""
|
| 218 |
-
# Veda Programming Assistant
|
| 219 |
|
| 220 |
I can chat, write code, explain concepts, and answer programming questions!
|
| 221 |
""")
|
| 222 |
|
| 223 |
with gr.Tabs():
|
| 224 |
|
| 225 |
-
with gr.TabItem("Chat"):
|
| 226 |
chatbot = gr.Chatbot(
|
| 227 |
label="Conversation",
|
| 228 |
-
height=400
|
| 229 |
-
type="messages"
|
| 230 |
)
|
| 231 |
|
| 232 |
with gr.Row():
|
|
@@ -255,29 +259,13 @@ with gr.Blocks(title="Veda Programming Assistant", theme=gr.themes.Soft()) as de
|
|
| 255 |
)
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
-
good_btn = gr.Button("Good
|
| 259 |
-
bad_btn = gr.Button("Bad
|
| 260 |
-
clear_btn = gr.Button("Clear
|
| 261 |
|
| 262 |
feedback_msg = gr.Textbox(label="Status", lines=1, interactive=False)
|
| 263 |
|
| 264 |
-
#
|
| 265 |
-
def chat_fn(user_input, history, temperature, max_tokens):
|
| 266 |
-
if not user_input.strip():
|
| 267 |
-
return "", history
|
| 268 |
-
|
| 269 |
-
response = generate_response(user_input, temperature, max_tokens)
|
| 270 |
-
|
| 271 |
-
if history is None:
|
| 272 |
-
history = []
|
| 273 |
-
|
| 274 |
-
history = history + [
|
| 275 |
-
{"role": "user", "content": user_input},
|
| 276 |
-
{"role": "assistant", "content": response}
|
| 277 |
-
]
|
| 278 |
-
|
| 279 |
-
return "", history
|
| 280 |
-
|
| 281 |
send_btn.click(
|
| 282 |
chat_fn,
|
| 283 |
inputs=[msg, chatbot, temperature, max_tokens],
|
|
@@ -292,29 +280,25 @@ with gr.Blocks(title="Veda Programming Assistant", theme=gr.themes.Soft()) as de
|
|
| 292 |
|
| 293 |
good_btn.click(feedback_good, outputs=feedback_msg)
|
| 294 |
bad_btn.click(feedback_bad, outputs=feedback_msg)
|
| 295 |
-
|
| 296 |
-
def clear_fn():
|
| 297 |
-
global conversation_history
|
| 298 |
-
conversation_history = []
|
| 299 |
-
return [], "Conversation cleared."
|
| 300 |
-
|
| 301 |
clear_btn.click(clear_fn, outputs=[chatbot, feedback_msg])
|
| 302 |
|
| 303 |
-
gr.Markdown("###
|
| 304 |
gr.Examples(
|
| 305 |
examples=[
|
| 306 |
-
"Hello! What can you do?",
|
| 307 |
-
"What is Python?",
|
| 308 |
-
"Write a function to calculate factorial",
|
| 309 |
-
"Explain what recursion is",
|
| 310 |
-
"How do I read a file in Python?",
|
| 311 |
-
"Write a bubble sort algorithm",
|
| 312 |
],
|
| 313 |
inputs=msg
|
| 314 |
)
|
| 315 |
|
| 316 |
-
with gr.TabItem("Training"):
|
| 317 |
gr.Markdown("### Train on approved conversations")
|
|
|
|
|
|
|
| 318 |
train_epochs = gr.Slider(
|
| 319 |
minimum=5,
|
| 320 |
maximum=20,
|
|
@@ -322,18 +306,26 @@ with gr.Blocks(title="Veda Programming Assistant", theme=gr.themes.Soft()) as de
|
|
| 322 |
step=1,
|
| 323 |
label="Epochs"
|
| 324 |
)
|
| 325 |
-
train_btn = gr.Button("Retrain Model", variant="primary")
|
| 326 |
train_output = gr.Markdown()
|
| 327 |
train_btn.click(retrain, inputs=[train_epochs], outputs=train_output)
|
| 328 |
|
| 329 |
-
with gr.TabItem("Statistics"):
|
| 330 |
stats_out = gr.Markdown()
|
| 331 |
-
refresh_btn = gr.Button("Refresh
|
| 332 |
refresh_btn.click(get_stats, outputs=stats_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
-
gr.Markdown("---\
|
| 335 |
|
| 336 |
|
| 337 |
-
# Launch
|
| 338 |
if __name__ == "__main__":
|
| 339 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
"""Gradio App for Veda Programming Assistant - Fixed for Gradio 6.2.0"""
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import tensorflow as tf
|
|
|
|
| 139 |
return f"Error: {str(e)}"
|
| 140 |
|
| 141 |
|
| 142 |
+
def chat_fn(user_input, history, temperature, max_tokens):
|
| 143 |
"""Chat function for Gradio"""
|
| 144 |
+
if not user_input or not user_input.strip():
|
| 145 |
+
return "", history or []
|
| 146 |
|
| 147 |
response = generate_response(user_input, temperature, max_tokens)
|
| 148 |
+
|
| 149 |
+
if history is None:
|
| 150 |
+
history = []
|
| 151 |
+
|
| 152 |
history = history + [[user_input, response]]
|
| 153 |
+
|
| 154 |
return "", history
|
| 155 |
|
| 156 |
|
|
|
|
| 168 |
return "No conversation to rate yet."
|
| 169 |
|
| 170 |
|
| 171 |
+
def clear_fn():
|
| 172 |
global conversation_history
|
| 173 |
conversation_history = []
|
| 174 |
return [], "Conversation cleared."
|
|
|
|
| 210 |
"""
|
| 211 |
|
| 212 |
|
| 213 |
+
# Initialize model at startup
|
| 214 |
print("Starting initialization...")
|
| 215 |
initialize()
|
| 216 |
print("Initialization complete!")
|
| 217 |
|
| 218 |
|
| 219 |
+
# Create Gradio interface - Fixed for Gradio 6.x
|
| 220 |
+
with gr.Blocks(title="Veda Programming Assistant") as demo:
|
| 221 |
|
| 222 |
gr.Markdown("""
|
| 223 |
+
# ποΈ Veda Programming Assistant
|
| 224 |
|
| 225 |
I can chat, write code, explain concepts, and answer programming questions!
|
| 226 |
""")
|
| 227 |
|
| 228 |
with gr.Tabs():
|
| 229 |
|
| 230 |
+
with gr.TabItem("π¬ Chat"):
|
| 231 |
chatbot = gr.Chatbot(
|
| 232 |
label="Conversation",
|
| 233 |
+
height=400
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
with gr.Row():
|
|
|
|
| 259 |
)
|
| 260 |
|
| 261 |
with gr.Row():
|
| 262 |
+
good_btn = gr.Button("π Good", variant="secondary")
|
| 263 |
+
bad_btn = gr.Button("π Bad", variant="secondary")
|
| 264 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
| 265 |
|
| 266 |
feedback_msg = gr.Textbox(label="Status", lines=1, interactive=False)
|
| 267 |
|
| 268 |
+
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
send_btn.click(
|
| 270 |
chat_fn,
|
| 271 |
inputs=[msg, chatbot, temperature, max_tokens],
|
|
|
|
| 280 |
|
| 281 |
good_btn.click(feedback_good, outputs=feedback_msg)
|
| 282 |
bad_btn.click(feedback_bad, outputs=feedback_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
clear_btn.click(clear_fn, outputs=[chatbot, feedback_msg])
|
| 284 |
|
| 285 |
+
gr.Markdown("### π‘ Example prompts")
|
| 286 |
gr.Examples(
|
| 287 |
examples=[
|
| 288 |
+
["Hello! What can you do?"],
|
| 289 |
+
["What is Python?"],
|
| 290 |
+
["Write a function to calculate factorial"],
|
| 291 |
+
["Explain what recursion is"],
|
| 292 |
+
["How do I read a file in Python?"],
|
| 293 |
+
["Write a bubble sort algorithm"],
|
| 294 |
],
|
| 295 |
inputs=msg
|
| 296 |
)
|
| 297 |
|
| 298 |
+
with gr.TabItem("π Training"):
|
| 299 |
gr.Markdown("### Train on approved conversations")
|
| 300 |
+
gr.Markdown("Rate responses as 'Good' to add them to training data.")
|
| 301 |
+
|
| 302 |
train_epochs = gr.Slider(
|
| 303 |
minimum=5,
|
| 304 |
maximum=20,
|
|
|
|
| 306 |
step=1,
|
| 307 |
label="Epochs"
|
| 308 |
)
|
| 309 |
+
train_btn = gr.Button("π Retrain Model", variant="primary")
|
| 310 |
train_output = gr.Markdown()
|
| 311 |
train_btn.click(retrain, inputs=[train_epochs], outputs=train_output)
|
| 312 |
|
| 313 |
+
with gr.TabItem("π Statistics"):
|
| 314 |
stats_out = gr.Markdown()
|
| 315 |
+
refresh_btn = gr.Button("π Refresh")
|
| 316 |
refresh_btn.click(get_stats, outputs=stats_out)
|
| 317 |
+
|
| 318 |
+
gr.Markdown("""
|
| 319 |
+
### How it works
|
| 320 |
+
1. Chat with the assistant
|
| 321 |
+
2. Rate responses as Good π or Bad π
|
| 322 |
+
3. Good responses are saved for training
|
| 323 |
+
4. Click 'Retrain Model' to improve the assistant
|
| 324 |
+
""")
|
| 325 |
|
| 326 |
+
gr.Markdown("---\n**Veda Programming Assistant** - Learning from conversations!")
|
| 327 |
|
| 328 |
|
| 329 |
+
# Launch
|
| 330 |
if __name__ == "__main__":
|
| 331 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|