Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from g4f.client import Client
|
| 4 |
+
import uvicorn
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
client = Client()
|
| 8 |
+
|
| 9 |
+
class ChatRequest(BaseModel):
|
| 10 |
+
message: str
|
| 11 |
+
model: str = "gpt-4.1"
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def root():
|
| 15 |
+
return {"status": "ok", "engine": "g4f"}
|
| 16 |
+
|
| 17 |
+
@app.post("/chat")
|
| 18 |
+
def chat(req: ChatRequest):
|
| 19 |
+
try:
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model=req.model,
|
| 22 |
+
messages=[{"role": "user", "content": req.message}],
|
| 23 |
+
web_search=False
|
| 24 |
+
)
|
| 25 |
+
return {
|
| 26 |
+
"reply": response.choices[0].message.content
|
| 27 |
+
}
|
| 28 |
+
except Exception as e:
|
| 29 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|