Spaces:
Sleeping
Sleeping
Commit ·
d0833a3
1
Parent(s): 390dd32
commit 00000022
Browse files
app.py
CHANGED
|
@@ -2,16 +2,15 @@ import os
|
|
| 2 |
import time
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
-
import
|
| 6 |
-
import gradio as gr
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from pydantic import BaseModel
|
| 10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 11 |
from huggingface_hub import login
|
| 12 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
# ✅ Safe GPU decorator
|
| 17 |
try:
|
|
@@ -28,7 +27,7 @@ app = FastAPI(
|
|
| 28 |
redoc_url="/redoc" # ReDoc at /redoc
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Enable CORS
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
| 34 |
allow_origins=["*"],
|
|
@@ -104,29 +103,6 @@ async def chat_stream(body: ChatRequest):
|
|
| 104 |
|
| 105 |
return StreamingResponse(generate(), media_type="text/plain")
|
| 106 |
|
| 107 |
-
# ---------------- Gradio UI ----------------
|
| 108 |
-
def gradio_chat(message, history):
|
| 109 |
-
# Convert Gradio format to your API format
|
| 110 |
-
formatted_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg}
|
| 111 |
-
for i, (msg, _) in enumerate(history)]
|
| 112 |
-
reply = generate_full_reply(message, formatted_history)
|
| 113 |
-
history.append((message, reply))
|
| 114 |
-
return history, ""
|
| 115 |
-
|
| 116 |
-
with gr.Blocks() as gradio_app:
|
| 117 |
-
gr.Markdown("# 🤖 ChatMate - Real-Time AI Assistant")
|
| 118 |
-
chatbot = gr.Chatbot()
|
| 119 |
-
msg = gr.Textbox(placeholder="Type your message here...")
|
| 120 |
-
clear = gr.Button("Clear Chat")
|
| 121 |
-
|
| 122 |
-
msg.submit(gradio_chat, [msg, chatbot], [chatbot, msg])
|
| 123 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 124 |
-
|
| 125 |
-
# Mount Gradio inside FastAPI
|
| 126 |
-
#from fastapi.middleware.wsgi import WSGIMiddleware
|
| 127 |
-
##app.mount("/", gr.mount_gradio_app(app, gradio_app, path="/"))
|
| 128 |
-
app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
|
| 129 |
-
|
| 130 |
# ---------------- Startup warm-up ----------------
|
| 131 |
@app.on_event("startup")
|
| 132 |
async def warmup_model():
|
|
@@ -134,6 +110,10 @@ async def warmup_model():
|
|
| 134 |
_ = generate_full_reply("Hello", [])
|
| 135 |
|
| 136 |
# ---------------- Run with Uvicorn ----------------
|
|
|
|
| 137 |
if __name__ == "__main__":
|
|
|
|
| 138 |
port = int(os.environ.get("PORT", 7860))
|
| 139 |
-
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import torch
|
| 4 |
import re
|
| 5 |
+
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
| 6 |
from fastapi.responses import StreamingResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 9 |
from huggingface_hub import login
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
import os
|
| 13 |
+
import uvicorn
|
| 14 |
|
| 15 |
# ✅ Safe GPU decorator
|
| 16 |
try:
|
|
|
|
| 27 |
redoc_url="/redoc" # ReDoc at /redoc
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Enable CORS (important for browser clients)
|
| 31 |
app.add_middleware(
|
| 32 |
CORSMiddleware,
|
| 33 |
allow_origins=["*"],
|
|
|
|
| 103 |
|
| 104 |
return StreamingResponse(generate(), media_type="text/plain")
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# ---------------- Startup warm-up ----------------
|
| 107 |
@app.on_event("startup")
|
| 108 |
async def warmup_model():
|
|
|
|
| 110 |
_ = generate_full_reply("Hello", [])
|
| 111 |
|
| 112 |
# ---------------- Run with Uvicorn ----------------
|
| 113 |
+
# In Hugging Face Spaces, just run: uvicorn app:app --host 0.0.0.0 --port 7860
|
| 114 |
if __name__ == "__main__":
|
| 115 |
+
# Hugging Face Spaces usually expects port 7860
|
| 116 |
port = int(os.environ.get("PORT", 7860))
|
| 117 |
+
|
| 118 |
+
# Run using uvicorn for FastAPI/Flask with ASGI wrapper
|
| 119 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)
|