R-Kentaren's picture
Upload app.py with huggingface_hub
2809502 verified
Raw
History Blame Contribute Delete
4.06 kB
import os
import json
import re
from pathlib import Path
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import gradio as gr
from openai import OpenAI
# Default system prompt for DeepSeek V4 Flash
DEFAULT_SYSTEM_PROMPT = "You are DeepSeek, a helpful AI assistant powered by DeepSeek V4 Flash. You provide accurate, detailed, and thoughtful responses."
# ── Initialize Gradio Server (FastAPI subclass) ─────────────────────────
app = gr.Server()
# Create static directory if it doesn't exist
STATIC_DIR = Path(__file__).parent / "static"
os.makedirs(STATIC_DIR, exist_ok=True)
# ── API Endpoint: Chat ──────────────────────────────────────────────────
@app.api(name="chat_with_deepseek")
def chat_with_deepseek(
messages_json: str,
reasoning_effort: str = "medium",
max_tokens: str = "2048",
temperature: str = "0.7",
system_prompt: str = "",
) -> str:
try:
messages = json.loads(messages_json)
max_tokens = int(max_tokens)
temperature = float(temperature)
key = os.environ.get("SILICONFLOW_API_KEY", "").strip()
if not key:
return json.dumps({
"status": "error",
"message": "SILICONFLOW_API_KEY environment variable is not configured on the server."
})
client = OpenAI(
api_key=key,
base_url="https://api.siliconflow.com/v1",
)
sys_prompt = system_prompt.strip() if system_prompt and system_prompt.strip() else DEFAULT_SYSTEM_PROMPT
messages_no_system = [m for m in messages if m.get("role") != "system"]
final_messages = [{"role": "system", "content": sys_prompt}] + messages_no_system
params = {
"model": "deepseek-ai/DeepSeek-V4-Flash",
"messages": final_messages,
"max_tokens": max_tokens,
"temperature": temperature
}
if reasoning_effort in ["low", "medium", "high"]:
params["reasoning_effort"] = reasoning_effort
response = client.chat.completions.create(**params)
content = response.choices[0].message.content
reasoning_content = getattr(response.choices[0].message, "reasoning_content", "")
if not reasoning_content and content and "<think" in content and "</think" in content:
think_match = re.search(r'<think[^>]*>(.*?)</think', content, re.DOTALL)
if think_match:
reasoning_content = think_match.group(1).strip()
content = re.sub(r'<think[^>]*>.*?</think\s*>', '', content, flags=re.DOTALL).strip()
return json.dumps({
"status": "success",
"content": content,
"reasoning_content": reasoning_content or ""
})
except Exception as e:
return json.dumps({
"status": "error",
"message": str(e)
})
# ── Serve the main HTML page ────────────────────────────────────────────
@app.get("/")
async def homepage():
html_path = STATIC_DIR / "index.html"
if html_path.exists():
with open(html_path, "r", encoding="utf-8") as f:
return HTMLResponse(content=f.read(), status_code=200)
return HTMLResponse(
content="<h1>Frontend is building. Please refresh in a few seconds...</h1>",
status_code=200
)
# ── Mount static folder for CSS, JS, and image assets ──────────────────
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
# ── Launch ──────────────────────────────────────────────────────────────
if __name__ == "__main__":
app.launch(show_error=True)