Spaces:
Build error
Build error
change architecture app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
from fastapi import FastAPI, HTTPException, Request
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import List, Optional
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
import spaces
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
# 1. Base FastAPI application (Ye sabhi incoming HTTP requests handle karegi)
|
| 11 |
-
api = FastAPI(title="Hermes 8B API")
|
| 12 |
-
|
| 13 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 14 |
|
| 15 |
print("Initializing Tokenizer...")
|
|
@@ -55,51 +53,64 @@ def generate_response(prompt_text: str, max_tokens: int, temperature: float):
|
|
| 55 |
generated_tokens = outputs[0][inputs.input_ids.shape[-1]:]
|
| 56 |
return tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 57 |
|
| 58 |
-
#
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
max_tokens=req.max_tokens,
|
| 77 |
-
temperature=req.temperature
|
| 78 |
-
)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
gr.Markdown("# ๐ Hermes-3 8B ZeroGPU API Server")
|
| 101 |
-
gr.Markdown("Active Endpoints:\n- `GET /health`\n- `POST /v1/chat/completions`")
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
|
| 105 |
-
app = gr.mount_gradio_app(api, demo, path="/gradio")
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
from fastapi import FastAPI, HTTPException, Request
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from typing import List, Optional
|
| 7 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 8 |
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
MODEL_ID = "NousResearch/Hermes-3-Llama-3.1-8B"
|
| 12 |
|
| 13 |
print("Initializing Tokenizer...")
|
|
|
|
| 53 |
generated_tokens = outputs[0][inputs.input_ids.shape[-1]:]
|
| 54 |
return tokenizer.decode(generated_tokens, skip_special_tokens=True)
|
| 55 |
|
| 56 |
+
# 1. Gradio Blocks banayein
|
| 57 |
+
with gr.Blocks(title="Hermes 8B Server") as demo:
|
| 58 |
+
gr.Markdown("# ๐ Hermes-3 8B ZeroGPU API Server")
|
| 59 |
+
gr.Markdown("Active Endpoints:\n- `GET /health`\n- `POST /v1/chat/completions`")
|
| 60 |
|
| 61 |
+
# 2. Gradio ke router par middleware level route interceptor lagayein
|
| 62 |
+
# Isse Gradio ka internal SPA router API routes ko HTML me convert nahi kar payega
|
| 63 |
+
@demo.app.middleware("http")
|
| 64 |
+
async def intercept_api_routes(request: Request, call_next):
|
| 65 |
+
# Health Route Intercept
|
| 66 |
+
if request.url.path == "/health" and request.method == "GET":
|
| 67 |
+
return JSONResponse(content={"status": "ok", "ready": True}, status_code=200)
|
| 68 |
+
|
| 69 |
+
# Completions Route Intercept
|
| 70 |
+
if request.url.path == "/v1/chat/completions" and request.method == "POST":
|
| 71 |
+
try:
|
| 72 |
+
body = await request.json()
|
| 73 |
+
messages = body.get("messages", [])
|
| 74 |
+
max_tokens = body.get("max_tokens", 256)
|
| 75 |
+
temperature = body.get("temperature", 0.7)
|
| 76 |
+
model_name = body.get("model", "hermes-3-8b")
|
| 77 |
|
| 78 |
+
formatted_messages = [{"role": m.get("role"), "content": m.get("content")} for m in messages]
|
| 79 |
+
prompt_text = tokenizer.apply_chat_template(
|
| 80 |
+
formatted_messages,
|
| 81 |
+
tokenize=False,
|
| 82 |
+
add_generation_prompt=True
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
completion_text = generate_response(
|
| 86 |
+
prompt_text=prompt_text,
|
| 87 |
+
max_tokens=max_tokens,
|
| 88 |
+
temperature=temperature
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
return JSONResponse(
|
| 92 |
+
status_code=200,
|
| 93 |
+
content={
|
| 94 |
+
"id": "chatcmpl-hermes",
|
| 95 |
+
"object": "chat.completion",
|
| 96 |
+
"model": model_name,
|
| 97 |
+
"choices": [
|
| 98 |
+
{
|
| 99 |
+
"index": 0,
|
| 100 |
+
"message": {
|
| 101 |
+
"role": "assistant",
|
| 102 |
+
"content": completion_text
|
| 103 |
+
},
|
| 104 |
+
"finish_reason": "stop"
|
| 105 |
+
}
|
| 106 |
+
]
|
| 107 |
}
|
| 108 |
+
)
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
| 111 |
|
| 112 |
+
# Baaki requests ko normal Gradio interface par pass hone dein
|
| 113 |
+
return await call_next(request)
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# 3. Native Gradio Launch (Container exit hone se rokta hai aur port bind karta hai)
|
| 116 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|