Vibe Coding
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import json
|
|
| 6 |
import torch
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from threading import Thread
|
|
|
|
| 9 |
|
| 10 |
# Gradio and HuggingFace imports
|
| 11 |
import gradio as gr
|
|
@@ -40,7 +41,6 @@ conversations = []
|
|
| 40 |
# Uncomment this line to login with your token
|
| 41 |
# login(token=os.environ.get("HF_TOKEN"))
|
| 42 |
|
| 43 |
-
|
| 44 |
def save_to_dataset():
|
| 45 |
"""Save the current conversations to a HuggingFace dataset"""
|
| 46 |
if not conversations:
|
|
@@ -77,17 +77,17 @@ def save_to_dataset():
|
|
| 77 |
|
| 78 |
return dataset, status_msg
|
| 79 |
|
| 80 |
-
|
| 81 |
@spaces.GPU(duration=120)
|
| 82 |
-
def
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
if conversation_id is None
|
| 86 |
conversation_id = str(uuid.uuid4())
|
|
|
|
| 87 |
|
| 88 |
# Format chat history for the model
|
| 89 |
formatted_history = []
|
| 90 |
-
for human_msg, ai_msg in
|
| 91 |
formatted_history.append({"role": "user", "content": human_msg})
|
| 92 |
if ai_msg: # Skip None values that might occur during streaming
|
| 93 |
formatted_history.append({"role": "assistant", "content": ai_msg})
|
|
@@ -126,14 +126,14 @@ def predict(message, chat_history, temperature, top_p, conversation_id=None):
|
|
| 126 |
# Yield partial text as it's generated
|
| 127 |
for new_text in streamer:
|
| 128 |
partial_text += new_text
|
| 129 |
-
yield
|
| 130 |
-
|
| 131 |
-
# Store conversation data
|
| 132 |
-
existing_conv = next((c for c in conversations if c["conversation_id"] == conversation_id), None)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
formatted_history.append({"role": "assistant", "content": partial_text})
|
| 136 |
|
|
|
|
|
|
|
|
|
|
| 137 |
# Update or create conversation record
|
| 138 |
current_time = datetime.now().isoformat()
|
| 139 |
if existing_conv:
|
|
@@ -162,16 +162,12 @@ def predict(message, chat_history, temperature, top_p, conversation_id=None):
|
|
| 162 |
if current_time_dt - last_save_time > timedelta(minutes=SAVE_INTERVAL_MINUTES):
|
| 163 |
save_to_dataset()
|
| 164 |
last_save_time = current_time_dt
|
| 165 |
-
|
| 166 |
-
return chat_history + [[message, partial_text]], conversation_id
|
| 167 |
-
|
| 168 |
|
| 169 |
def save_dataset_manually():
|
| 170 |
-
"""Manually trigger dataset save"""
|
| 171 |
_, status = save_to_dataset()
|
| 172 |
return status
|
| 173 |
|
| 174 |
-
|
| 175 |
def get_stats():
|
| 176 |
"""Get current stats about conversations and saving"""
|
| 177 |
mins_until_save = SAVE_INTERVAL_MINUTES - (datetime.now() - last_save_time).seconds // 60
|
|
@@ -185,8 +181,7 @@ def get_stats():
|
|
| 185 |
"dataset_name": DATASET_NAME
|
| 186 |
}
|
| 187 |
|
| 188 |
-
|
| 189 |
-
# Create a Stanford theme using the simpler approach from Gradio examples
|
| 190 |
theme = gr.themes.Default(
|
| 191 |
primary_hue=gr.themes.utils.colors.red,
|
| 192 |
secondary_hue=gr.themes.utils.colors.gray,
|
|
@@ -207,7 +202,7 @@ theme = gr.themes.Default(
|
|
| 207 |
block_label_background_fill="#f9f9f9"
|
| 208 |
)
|
| 209 |
|
| 210 |
-
# Custom CSS
|
| 211 |
css = """
|
| 212 |
.gradio-container {
|
| 213 |
font-family: 'Source Sans Pro', sans-serif !important;
|
|
@@ -218,41 +213,47 @@ css = """
|
|
| 218 |
}
|
| 219 |
"""
|
| 220 |
|
| 221 |
-
# Set up the Gradio app
|
| 222 |
with gr.Blocks(theme=theme, title="Stanford Soft Raccoon Chat", css=css) as demo:
|
| 223 |
-
conversation_id = gr.State("")
|
| 224 |
-
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column(scale=3):
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
)
|
| 232 |
-
|
| 233 |
-
with gr.Row():
|
| 234 |
-
msg = gr.Textbox(
|
| 235 |
-
placeholder="Send a message...",
|
| 236 |
-
show_label=False,
|
| 237 |
-
container=False
|
| 238 |
-
)
|
| 239 |
-
submit_btn = gr.Button("Send", variant="primary")
|
| 240 |
-
|
| 241 |
-
with gr.Accordion("Generation Parameters", open=False):
|
| 242 |
-
temperature = gr.Slider(
|
| 243 |
-
minimum=0.1,
|
| 244 |
-
maximum=2.0,
|
| 245 |
-
value=0.7,
|
| 246 |
-
step=0.1,
|
| 247 |
-
label="Temperature"
|
| 248 |
-
)
|
| 249 |
-
top_p = gr.Slider(
|
| 250 |
-
minimum=0.1,
|
| 251 |
-
maximum=1.0,
|
| 252 |
-
value=0.9,
|
| 253 |
-
step=0.05,
|
| 254 |
-
label="Top-P"
|
| 255 |
-
)
|
| 256 |
|
| 257 |
with gr.Column(scale=1):
|
| 258 |
with gr.Group():
|
|
@@ -270,20 +271,6 @@ with gr.Blocks(theme=theme, title="Stanford Soft Raccoon Chat", css=css) as demo
|
|
| 270 |
refresh_btn = gr.Button("Refresh Stats")
|
| 271 |
|
| 272 |
# Set up event handlers
|
| 273 |
-
submit_btn.click(
|
| 274 |
-
predict,
|
| 275 |
-
[msg, chatbot, temperature, top_p, conversation_id],
|
| 276 |
-
[chatbot, conversation_id],
|
| 277 |
-
api_name="chat"
|
| 278 |
-
)
|
| 279 |
-
|
| 280 |
-
msg.submit(
|
| 281 |
-
predict,
|
| 282 |
-
[msg, chatbot, temperature, top_p, conversation_id],
|
| 283 |
-
[chatbot, conversation_id],
|
| 284 |
-
api_name=False
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
save_button.click(
|
| 288 |
save_dataset_manually,
|
| 289 |
[],
|
|
@@ -309,10 +296,11 @@ with gr.Blocks(theme=theme, title="Stanford Soft Raccoon Chat", css=css) as demo
|
|
| 309 |
demo.load(
|
| 310 |
update_stats,
|
| 311 |
[],
|
| 312 |
-
[convo_count, next_save, last_save_time_display, dataset_name_display]
|
|
|
|
| 313 |
)
|
| 314 |
|
| 315 |
-
# Ensure we save on shutdown
|
| 316 |
import atexit
|
| 317 |
atexit.register(save_to_dataset)
|
| 318 |
|
|
|
|
| 6 |
import torch
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from threading import Thread
|
| 9 |
+
from pathlib import Path
|
| 10 |
|
| 11 |
# Gradio and HuggingFace imports
|
| 12 |
import gradio as gr
|
|
|
|
| 41 |
# Uncomment this line to login with your token
|
| 42 |
# login(token=os.environ.get("HF_TOKEN"))
|
| 43 |
|
|
|
|
| 44 |
def save_to_dataset():
|
| 45 |
"""Save the current conversations to a HuggingFace dataset"""
|
| 46 |
if not conversations:
|
|
|
|
| 77 |
|
| 78 |
return dataset, status_msg
|
| 79 |
|
|
|
|
| 80 |
@spaces.GPU(duration=120)
|
| 81 |
+
def chat_model(message, history, temperature=0.7, top_p=0.9):
|
| 82 |
+
"""Chat function for use with ChatInterface"""
|
| 83 |
+
conversation_id = getattr(chat_model, "conversation_id", None)
|
| 84 |
+
if conversation_id is None:
|
| 85 |
conversation_id = str(uuid.uuid4())
|
| 86 |
+
chat_model.conversation_id = conversation_id
|
| 87 |
|
| 88 |
# Format chat history for the model
|
| 89 |
formatted_history = []
|
| 90 |
+
for human_msg, ai_msg in history:
|
| 91 |
formatted_history.append({"role": "user", "content": human_msg})
|
| 92 |
if ai_msg: # Skip None values that might occur during streaming
|
| 93 |
formatted_history.append({"role": "assistant", "content": ai_msg})
|
|
|
|
| 126 |
# Yield partial text as it's generated
|
| 127 |
for new_text in streamer:
|
| 128 |
partial_text += new_text
|
| 129 |
+
yield partial_text
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# Store conversation data in the global conversations list
|
| 132 |
formatted_history.append({"role": "assistant", "content": partial_text})
|
| 133 |
|
| 134 |
+
# Find existing conversation or create new one
|
| 135 |
+
existing_conv = next((c for c in conversations if c["conversation_id"] == conversation_id), None)
|
| 136 |
+
|
| 137 |
# Update or create conversation record
|
| 138 |
current_time = datetime.now().isoformat()
|
| 139 |
if existing_conv:
|
|
|
|
| 162 |
if current_time_dt - last_save_time > timedelta(minutes=SAVE_INTERVAL_MINUTES):
|
| 163 |
save_to_dataset()
|
| 164 |
last_save_time = current_time_dt
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def save_dataset_manually():
|
| 167 |
+
"""Manually trigger dataset save and return status"""
|
| 168 |
_, status = save_to_dataset()
|
| 169 |
return status
|
| 170 |
|
|
|
|
| 171 |
def get_stats():
|
| 172 |
"""Get current stats about conversations and saving"""
|
| 173 |
mins_until_save = SAVE_INTERVAL_MINUTES - (datetime.now() - last_save_time).seconds // 60
|
|
|
|
| 181 |
"dataset_name": DATASET_NAME
|
| 182 |
}
|
| 183 |
|
| 184 |
+
# Create a Stanford theme
|
|
|
|
| 185 |
theme = gr.themes.Default(
|
| 186 |
primary_hue=gr.themes.utils.colors.red,
|
| 187 |
secondary_hue=gr.themes.utils.colors.gray,
|
|
|
|
| 202 |
block_label_background_fill="#f9f9f9"
|
| 203 |
)
|
| 204 |
|
| 205 |
+
# Custom CSS
|
| 206 |
css = """
|
| 207 |
.gradio-container {
|
| 208 |
font-family: 'Source Sans Pro', sans-serif !important;
|
|
|
|
| 213 |
}
|
| 214 |
"""
|
| 215 |
|
| 216 |
+
# Set up the Gradio app with Blocks for more control
|
| 217 |
with gr.Blocks(theme=theme, title="Stanford Soft Raccoon Chat", css=css) as demo:
|
|
|
|
|
|
|
| 218 |
with gr.Row():
|
| 219 |
with gr.Column(scale=3):
|
| 220 |
+
# Use ChatInterface for the main chat functionality
|
| 221 |
+
chatbot = gr.ChatInterface(
|
| 222 |
+
fn=chat_model,
|
| 223 |
+
chatbot=gr.Chatbot(
|
| 224 |
+
label="Soft Raccoon Chat",
|
| 225 |
+
avatar_images=(None, "🌲"), # Stanford tree emoji
|
| 226 |
+
height=600,
|
| 227 |
+
placeholder="<strong>Soft Raccoon AI Assistant</strong><br>Ask me anything!"
|
| 228 |
+
),
|
| 229 |
+
additional_inputs=[
|
| 230 |
+
gr.Slider(
|
| 231 |
+
minimum=0.1,
|
| 232 |
+
maximum=2.0,
|
| 233 |
+
value=0.7,
|
| 234 |
+
step=0.1,
|
| 235 |
+
label="Temperature"
|
| 236 |
+
),
|
| 237 |
+
gr.Slider(
|
| 238 |
+
minimum=0.1,
|
| 239 |
+
maximum=1.0,
|
| 240 |
+
value=0.9,
|
| 241 |
+
step=0.05,
|
| 242 |
+
label="Top-P"
|
| 243 |
+
)
|
| 244 |
+
],
|
| 245 |
+
title="Stanford Soft Raccoon Chat",
|
| 246 |
+
description="AI assistant powered by the Soft Raccoon language model",
|
| 247 |
+
examples=[
|
| 248 |
+
"Tell me about Stanford University",
|
| 249 |
+
"How can I learn about artificial intelligence?",
|
| 250 |
+
"What's your favorite book?"
|
| 251 |
+
],
|
| 252 |
+
cache_examples=True,
|
| 253 |
+
retry_btn="Regenerate",
|
| 254 |
+
undo_btn="Undo",
|
| 255 |
+
clear_btn="Clear",
|
| 256 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
with gr.Column(scale=1):
|
| 259 |
with gr.Group():
|
|
|
|
| 271 |
refresh_btn = gr.Button("Refresh Stats")
|
| 272 |
|
| 273 |
# Set up event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
save_button.click(
|
| 275 |
save_dataset_manually,
|
| 276 |
[],
|
|
|
|
| 296 |
demo.load(
|
| 297 |
update_stats,
|
| 298 |
[],
|
| 299 |
+
[convo_count, next_save, last_save_time_display, dataset_name_display],
|
| 300 |
+
every=30 # Refresh every 30 seconds
|
| 301 |
)
|
| 302 |
|
| 303 |
+
# Ensure we save on shutdown
|
| 304 |
import atexit
|
| 305 |
atexit.register(save_to_dataset)
|
| 306 |
|