Spaces:
Sleeping
Sleeping
Add initial FastAPI application and requirements
Browse files- app.py +30 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
JSONL_FILE = "feedback_dataset.jsonl"
|
| 9 |
+
|
| 10 |
+
class Feedback(BaseModel):
|
| 11 |
+
question: str
|
| 12 |
+
answer: str
|
| 13 |
+
rating: int
|
| 14 |
+
|
| 15 |
+
@app.post("/rate")
|
| 16 |
+
def rate_feedback(feedback: Feedback):
|
| 17 |
+
if feedback.rating < 1 or feedback.rating > 5:
|
| 18 |
+
raise HTTPException(status_code=400, detail="Rating must be between 1 and 5")
|
| 19 |
+
|
| 20 |
+
record = {
|
| 21 |
+
"timestamp": datetime.now().isoformat(),
|
| 22 |
+
"question": feedback.question.strip(),
|
| 23 |
+
"answer": feedback.answer.strip(),
|
| 24 |
+
"rating": int(feedback.rating)
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
with open(JSONL_FILE, "a", encoding="utf-8") as f:
|
| 28 |
+
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
| 29 |
+
|
| 30 |
+
return {"message": "✅ Feedback saved"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|