Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import gradio as gr
|
2 |
from fastapi import FastAPI, Request, HTTPException
|
3 |
from fastapi.responses import JSONResponse
|
4 |
import datetime
|
@@ -92,54 +91,4 @@ async def chat_completion(request: Request):
|
|
92 |
raise e
|
93 |
except Exception as e:
|
94 |
logger.error(f"Unexpected error: {e}")
|
95 |
-
raise HTTPException(status_code=500, detail=str(e))
|
96 |
-
|
97 |
-
def generate_response(messages):
|
98 |
-
payload = {
|
99 |
-
"inputs": {
|
100 |
-
"messages": messages
|
101 |
-
},
|
102 |
-
"parameters": {
|
103 |
-
"max_new_tokens": 2048,
|
104 |
-
"temperature": 0.7,
|
105 |
-
"top_p": 0.95,
|
106 |
-
"do_sample": True
|
107 |
-
}
|
108 |
-
}
|
109 |
-
|
110 |
-
try:
|
111 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
112 |
-
response.raise_for_status()
|
113 |
-
result = response.json()
|
114 |
-
|
115 |
-
if isinstance(result, dict) and "error" in result:
|
116 |
-
return f"Error: {result['error']}"
|
117 |
-
|
118 |
-
return result[0]["generated_text"]
|
119 |
-
except requests.exceptions.RequestException as e:
|
120 |
-
logger.error(f"Request failed: {e}")
|
121 |
-
return f"Error: {e}"
|
122 |
-
|
123 |
-
def chat_interface(messages):
|
124 |
-
chat_history = []
|
125 |
-
for message in messages:
|
126 |
-
try:
|
127 |
-
response = generate_response([{"role": "user", "content": message}])
|
128 |
-
chat_history.append({"role": "user", "content": message})
|
129 |
-
chat_history.append({"role": "assistant", "content": response})
|
130 |
-
except Exception as e:
|
131 |
-
chat_history.append({"role": "user", "content": message})
|
132 |
-
chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
|
133 |
-
return chat_history
|
134 |
-
|
135 |
-
# Create Gradio interface
|
136 |
-
def gradio_app():
|
137 |
-
return gr.ChatInterface(chat_interface, type="messages")
|
138 |
-
|
139 |
-
# Mount both FastAPI and Gradio
|
140 |
-
app = gr.mount_gradio_app(app, gradio_app(), path="/")
|
141 |
-
|
142 |
-
# For running with uvicorn directly
|
143 |
-
if __name__ == "__main__":
|
144 |
-
import uvicorn
|
145 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
1 |
from fastapi import FastAPI, Request, HTTPException
|
2 |
from fastapi.responses import JSONResponse
|
3 |
import datetime
|
|
|
91 |
raise e
|
92 |
except Exception as e:
|
93 |
logger.error(f"Unexpected error: {e}")
|
94 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|