Spaces:
No application file
No application file
Download app.py from nicebeam/semantic-rag: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/nicebeam/semantic-rag/resolve/main/app.py
- Command line
-
hf download hf://spaces/nicebeam/semantic-rag/app.py
-
curl -L -o app.py https://huggingface.co/spaces/nicebeam/semantic-rag/resolve/main/app.py
1.1 kB
| """Semantic RAG - Simplified for HF Spaces""" | |
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from openai import OpenAI | |
| app = FastAPI(title="Semantic RAG DB") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| class SearchRequest(BaseModel): | |
| query: str | |
| class SearchResponse(BaseModel): | |
| answer: str | |
| def root(): | |
| return {"status": "running", "message": "Semantic RAG API"} | |
| def health(): | |
| return {"status": "healthy"} | |
| def search(req: SearchRequest): | |
| if not os.getenv("OPENAI_API_KEY"): | |
| raise HTTPException(400, "OPENAI_API_KEY not set") | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[{"role": "user", "content": req.query}], | |
| max_tokens=1000, | |
| ) | |
| return SearchResponse(answer=response.choices[0].message.content) | |