Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Request, HTTPException
|
2 |
from fastapi.responses import JSONResponse
|
3 |
import datetime
|
@@ -15,7 +24,7 @@ logger = logging.getLogger(__name__)
|
|
15 |
# Configuration
|
16 |
API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B"
|
17 |
headers = {
|
18 |
-
"Authorization": f"Bearer {
|
19 |
"Content-Type": "application/json"
|
20 |
}
|
21 |
|
@@ -93,13 +102,52 @@ async def chat_completion(request: Request):
|
|
93 |
logger.error(f"Unexpected error: {e}")
|
94 |
raise HTTPException(status_code=500, detail=str(e))
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
def chat_interface(messages):
|
98 |
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# Create Gradio interface
|
101 |
def gradio_app():
|
102 |
-
#return gr.chat_interface(gr.Chatbot(placeholder="placeholder"), type="messages", value=[])
|
103 |
return gr.ChatInterface(chat_interface, type="messages")
|
104 |
|
105 |
-
# Mount both FastAPI and Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastapi import FastAPI, Request
|
3 |
+
from fastapi.responses import JSONResponse
|
4 |
+
import datetime
|
5 |
+
import requests
|
6 |
+
import os
|
7 |
+
import json
|
8 |
+
import asyncio
|
9 |
+
|
10 |
from fastapi import FastAPI, Request, HTTPException
|
11 |
from fastapi.responses import JSONResponse
|
12 |
import datetime
|
|
|
24 |
# Configuration
|
25 |
API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B"
|
26 |
headers = {
|
27 |
+
"Authorization": f"Bearer {$(cat /run/secrets/SECRET_EXAMPLE)}",
|
28 |
"Content-Type": "application/json"
|
29 |
}
|
30 |
|
|
|
102 |
logger.error(f"Unexpected error: {e}")
|
103 |
raise HTTPException(status_code=500, detail=str(e))
|
104 |
|
105 |
+
def generate_response(messages):
|
106 |
+
payload = {
|
107 |
+
"inputs": {
|
108 |
+
"messages": messages
|
109 |
+
},
|
110 |
+
"parameters": {
|
111 |
+
"max_new_tokens": 2048,
|
112 |
+
"temperature": 0.7,
|
113 |
+
"top_p": 0.95,
|
114 |
+
"do_sample": True
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
try:
|
119 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
120 |
+
response.raise_for_status()
|
121 |
+
result = response.json()
|
122 |
+
|
123 |
+
if isinstance(result, dict) and "error" in result:
|
124 |
+
return f"Error: {result['error']}"
|
125 |
+
|
126 |
+
return result[0]["generated_text"]
|
127 |
+
except requests.exceptions.RequestException as e:
|
128 |
+
logger.error(f"Request failed: {e}")
|
129 |
+
return f"Error: {e}"
|
130 |
|
131 |
def chat_interface(messages):
|
132 |
chat_history = []
|
133 |
+
for message in messages:
|
134 |
+
try:
|
135 |
+
response = generate_response([{"role": "user", "content": message}])
|
136 |
+
chat_history.append({"role": "user", "content": message})
|
137 |
+
chat_history.append({"role": "assistant", "content": response})
|
138 |
+
except Exception as e:
|
139 |
+
chat_history.append({"role": "user", "content": message})
|
140 |
+
chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
141 |
+
return chat_history
|
142 |
|
143 |
# Create Gradio interface
|
144 |
def gradio_app():
|
|
|
145 |
return gr.ChatInterface(chat_interface, type="messages")
|
146 |
|
147 |
+
# Mount both FastAPI and Gradio
|
148 |
+
app = gr.mount_gradio_app(app, gradio_app(), path="/")
|
149 |
+
|
150 |
+
# For running with uvicorn directly
|
151 |
+
if __name__ == "__main__":
|
152 |
+
import uvicorn
|
153 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|