Spaces:
Sleeping
Sleeping
hello
Browse files- .dockerignore +0 -0
- Dockerfile +1 -1
- main.py +0 -164
- requirements.txt +0 -1
.dockerignore
DELETED
|
File without changes
|
Dockerfile
CHANGED
|
@@ -37,7 +37,7 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
|
|
| 37 |
HUGGINGFACE_HUB_CACHE=/app/hf_cache
|
| 38 |
|
| 39 |
# Create cache directories with proper permissions
|
| 40 |
-
RUN mkdir -p /app/model_cache /app/hf_cache
|
| 41 |
|
| 42 |
# Copy application code
|
| 43 |
COPY . .
|
|
|
|
| 37 |
HUGGINGFACE_HUB_CACHE=/app/hf_cache
|
| 38 |
|
| 39 |
# Create cache directories with proper permissions
|
| 40 |
+
RUN mkdir -p /app/model_cache /app/hf_cache
|
| 41 |
|
| 42 |
# Copy application code
|
| 43 |
COPY . .
|
main.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
|
@@ -254,154 +253,6 @@ async def get_status():
|
|
| 254 |
"endpoints": ["/", "/health", "/chat", "/model-info", "/docs"]
|
| 255 |
}
|
| 256 |
|
| 257 |
-
# Mount static files if directory exists
|
| 258 |
-
if Path("static").exists():
|
| 259 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 260 |
-
|
| 261 |
-
# Gradio interface
|
| 262 |
-
def chat_with_bot(message, history, max_length, temperature, top_p):
|
| 263 |
-
"""Gradio chat function with advanced parameters"""
|
| 264 |
-
if not message.strip():
|
| 265 |
-
return "Please enter a message."
|
| 266 |
-
|
| 267 |
-
response_text, _ = generate_response(message.strip(), max_length, temperature, top_p)
|
| 268 |
-
return response_text
|
| 269 |
-
|
| 270 |
-
def create_gradio_interface():
|
| 271 |
-
"""Create enhanced Gradio interface"""
|
| 272 |
-
|
| 273 |
-
# Custom CSS
|
| 274 |
-
css = """
|
| 275 |
-
.gradio-container {
|
| 276 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 277 |
-
max-width: 1200px;
|
| 278 |
-
margin: 0 auto;
|
| 279 |
-
}
|
| 280 |
-
.chat-message {
|
| 281 |
-
font-size: 14px !important;
|
| 282 |
-
line-height: 1.4;
|
| 283 |
-
}
|
| 284 |
-
.gradio-chatbot {
|
| 285 |
-
height: 500px;
|
| 286 |
-
}
|
| 287 |
-
"""
|
| 288 |
-
|
| 289 |
-
# Create interface with advanced controls
|
| 290 |
-
with gr.Blocks(css=css, title="FastAPI Chatbot", theme=gr.themes.Soft()) as demo:
|
| 291 |
-
|
| 292 |
-
gr.HTML("<h1 style='text-align: center; color: #2563eb;'>🤖 FastAPI Chatbot</h1>")
|
| 293 |
-
gr.HTML(f"<p style='text-align: center; color: #6b7280;'>Powered by {MODEL_NAME}</p>")
|
| 294 |
-
|
| 295 |
-
with gr.Row():
|
| 296 |
-
with gr.Column(scale=3):
|
| 297 |
-
chatbot = gr.Chatbot(
|
| 298 |
-
height=500,
|
| 299 |
-
show_copy_button=True,
|
| 300 |
-
bubble_full_width=False,
|
| 301 |
-
avatar_images=("👤", "🤖")
|
| 302 |
-
)
|
| 303 |
-
|
| 304 |
-
with gr.Row():
|
| 305 |
-
msg = gr.Textbox(
|
| 306 |
-
placeholder="Type your message here...",
|
| 307 |
-
container=False,
|
| 308 |
-
scale=4,
|
| 309 |
-
max_lines=3
|
| 310 |
-
)
|
| 311 |
-
submit_btn = gr.Button("Send 📤", scale=1, variant="primary")
|
| 312 |
-
|
| 313 |
-
with gr.Row():
|
| 314 |
-
clear_btn = gr.Button("Clear Chat 🗑️", scale=1)
|
| 315 |
-
retry_btn = gr.Button("Retry Last ↻", scale=1)
|
| 316 |
-
|
| 317 |
-
with gr.Column(scale=1):
|
| 318 |
-
gr.HTML("<h3>Settings</h3>")
|
| 319 |
-
|
| 320 |
-
max_length = gr.Slider(
|
| 321 |
-
minimum=50,
|
| 322 |
-
maximum=200,
|
| 323 |
-
value=MAX_LENGTH,
|
| 324 |
-
step=10,
|
| 325 |
-
label="Max Response Length"
|
| 326 |
-
)
|
| 327 |
-
|
| 328 |
-
temperature = gr.Slider(
|
| 329 |
-
minimum=0.1,
|
| 330 |
-
maximum=1.5,
|
| 331 |
-
value=DEFAULT_TEMPERATURE,
|
| 332 |
-
step=0.1,
|
| 333 |
-
label="Temperature (Creativity)"
|
| 334 |
-
)
|
| 335 |
-
|
| 336 |
-
top_p = gr.Slider(
|
| 337 |
-
minimum=0.1,
|
| 338 |
-
maximum=1.0,
|
| 339 |
-
value=0.9,
|
| 340 |
-
step=0.05,
|
| 341 |
-
label="Top-p (Focus)"
|
| 342 |
-
)
|
| 343 |
-
|
| 344 |
-
gr.HTML("<h4>Example Messages:</h4>")
|
| 345 |
-
examples = gr.Examples(
|
| 346 |
-
examples=[
|
| 347 |
-
["Hello! How are you today?"],
|
| 348 |
-
["Tell me a joke"],
|
| 349 |
-
["What's your favorite hobby?"],
|
| 350 |
-
["Can you help me with a creative writing prompt?"],
|
| 351 |
-
["What do you think about technology?"]
|
| 352 |
-
],
|
| 353 |
-
inputs=msg,
|
| 354 |
-
label="Click to try:"
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
# Event handlers
|
| 358 |
-
def respond(message, history, max_len, temp, top_p):
|
| 359 |
-
if not message.strip():
|
| 360 |
-
return history, ""
|
| 361 |
-
|
| 362 |
-
# Add user message
|
| 363 |
-
history.append([message, None])
|
| 364 |
-
|
| 365 |
-
# Generate bot response
|
| 366 |
-
bot_response = chat_with_bot(message, history, max_len, temp, top_p)
|
| 367 |
-
history[-1][1] = bot_response
|
| 368 |
-
|
| 369 |
-
return history, ""
|
| 370 |
-
|
| 371 |
-
def clear_chat():
|
| 372 |
-
return [], ""
|
| 373 |
-
|
| 374 |
-
def retry_last(history, max_len, temp, top_p):
|
| 375 |
-
if not history:
|
| 376 |
-
return history
|
| 377 |
-
|
| 378 |
-
last_user_msg = history[-1][0]
|
| 379 |
-
history[-1][1] = "Thinking..."
|
| 380 |
-
|
| 381 |
-
# Regenerate response
|
| 382 |
-
bot_response = chat_with_bot(last_user_msg, history, max_len, temp, top_p)
|
| 383 |
-
history[-1][1] = bot_response
|
| 384 |
-
|
| 385 |
-
return history
|
| 386 |
-
|
| 387 |
-
# Wire up events
|
| 388 |
-
submit_btn.click(
|
| 389 |
-
respond,
|
| 390 |
-
[msg, chatbot, max_length, temperature, top_p],
|
| 391 |
-
[chatbot, msg]
|
| 392 |
-
)
|
| 393 |
-
|
| 394 |
-
msg.submit(
|
| 395 |
-
respond,
|
| 396 |
-
[msg, chatbot, max_length, temperature, top_p],
|
| 397 |
-
[chatbot, msg]
|
| 398 |
-
)
|
| 399 |
-
|
| 400 |
-
clear_btn.click(clear_chat, outputs=[chatbot, msg])
|
| 401 |
-
retry_btn.click(retry_last, [chatbot, max_length, temperature, top_p], chatbot)
|
| 402 |
-
|
| 403 |
-
return demo
|
| 404 |
-
|
| 405 |
def run_fastapi():
|
| 406 |
"""Run FastAPI server"""
|
| 407 |
uvicorn.run(
|
|
@@ -426,25 +277,10 @@ def main():
|
|
| 426 |
|
| 427 |
logger.info("✅ Model loaded successfully!")
|
| 428 |
|
| 429 |
-
# Create Gradio interface
|
| 430 |
-
logger.info("🎨 Creating Gradio interface...")
|
| 431 |
-
demo = create_gradio_interface()
|
| 432 |
-
|
| 433 |
# Start FastAPI server in a separate thread
|
| 434 |
logger.info("🌐 Starting FastAPI server...")
|
| 435 |
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 436 |
fastapi_thread.start()
|
| 437 |
-
|
| 438 |
-
# Launch Gradio interface
|
| 439 |
-
logger.info("🚀 Launching Gradio interface...")
|
| 440 |
-
demo.launch(
|
| 441 |
-
server_name="0.0.0.0",
|
| 442 |
-
server_port=7860,
|
| 443 |
-
share=False,
|
| 444 |
-
show_error=True,
|
| 445 |
-
quiet=False,
|
| 446 |
-
show_api=False
|
| 447 |
-
)
|
| 448 |
|
| 449 |
if __name__ == "__main__":
|
| 450 |
main()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 253 |
"endpoints": ["/", "/health", "/chat", "/model-info", "/docs"]
|
| 254 |
}
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
def run_fastapi():
|
| 257 |
"""Run FastAPI server"""
|
| 258 |
uvicorn.run(
|
|
|
|
| 277 |
|
| 278 |
logger.info("✅ Model loaded successfully!")
|
| 279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
# Start FastAPI server in a separate thread
|
| 281 |
logger.info("🌐 Starting FastAPI server...")
|
| 282 |
fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
|
| 283 |
fastapi_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
if __name__ == "__main__":
|
| 286 |
main()
|
requirements.txt
CHANGED
|
@@ -4,7 +4,6 @@ transformers==4.35.2
|
|
| 4 |
torch==2.1.0
|
| 5 |
tokenizers==0.15.0
|
| 6 |
accelerate==0.24.1
|
| 7 |
-
gradio==4.7.1
|
| 8 |
requests==2.31.0
|
| 9 |
numpy==1.24.3
|
| 10 |
pydantic==2.4.2
|
|
|
|
| 4 |
torch==2.1.0
|
| 5 |
tokenizers==0.15.0
|
| 6 |
accelerate==0.24.1
|
|
|
|
| 7 |
requests==2.31.0
|
| 8 |
numpy==1.24.3
|
| 9 |
pydantic==2.4.2
|