Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import random
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# Enable CORS for JavaScript fetch usage across origins
|
9 |
+
app.add_middleware(
|
10 |
+
CORSMiddleware,
|
11 |
+
allow_origins=["*"],
|
12 |
+
allow_credentials=False,
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"]
|
15 |
+
)
|
16 |
+
|
17 |
+
class QueryRequest(BaseModel):
|
18 |
+
query: str
|
19 |
+
echo: bool = False
|
20 |
+
|
21 |
+
@app.post("/api/query")
|
22 |
+
async def general_query(req: QueryRequest):
|
23 |
+
text = req.query.strip()
|
24 |
+
if not text:
|
25 |
+
raise HTTPException(status_code=400, detail="Query must not be empty")
|
26 |
+
if req.echo:
|
27 |
+
return {"response": text}
|
28 |
+
words = text.split()
|
29 |
+
random.shuffle(words)
|
30 |
+
return {"response": " ".join(words)}
|