File size: 1,373 Bytes
44d337e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
FastAPI + Gradio combo app for Hugging Face Space
"""
import os, gradio as gr
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
from rag import retrieve_info     # your existing function

# -------- 1)  Private key list ----------
VALID_KEYS = set(os.getenv("RAG_KEYS", "alpha,beta").split(","))  # store secret in Space > Settings > Secrets

# -------- 2)  FastAPI core app ----------
app = FastAPI(title="Moffitt RAG API")

class QueryIn(BaseModel):
    query: str
    k: int = 5

@app.post("/v1/query")
async def rag_query(body: QueryIn, x_api_key: str = Header(None)):
    """
    Secure JSON endpoint.
    Caller must send:  X-API-Key: <one-of-valid-keys>
    """
    if x_api_key not in VALID_KEYS:
        raise HTTPException(status_code=401, detail="Invalid or missing X-API-Key")

    text = retrieve_info(body.query, body.k)
    return {"answer": text}

# -------- 3)  Public Gradio UI ----------
def run(q, k):
    return retrieve_info(q, int(k))

demo = gr.Interface(
    fn=run,
    inputs=["text", gr.Number(label="k (Number of chunks to retrieve)")],
    outputs=gr.Textbox(lines=25, label="Retrieved chunks"),
    allow_flagging="never",
    title="Moffitt RAG Demo",
    description="Type a question; we search Chroma with E5 embeddings."
)

# Mount Gradio at ROOT path "/"
if __name__ == "__main__":
    demo.launch()