Spaces:
Running
Running
BuildNg
commited on
Commit
·
195140d
1
Parent(s):
44d337e
changes
Browse files
README.md
DELETED
@@ -1,11 +0,0 @@
|
|
1 |
-
---
|
2 |
-
title: Moffitt Rag Demo
|
3 |
-
emoji: 💬
|
4 |
-
colorFrom: yellow
|
5 |
-
colorTo: purple
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 5.0.1
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
short_description: API Hosting
|
11 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -1,47 +1,43 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
"""
|
4 |
-
import os, gradio as gr
|
5 |
from fastapi import FastAPI, Header, HTTPException
|
6 |
from pydantic import BaseModel
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
VALID_KEYS = set(os.getenv("RAG_KEYS", "alpha
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
|
15 |
class QueryIn(BaseModel):
|
16 |
query: str
|
17 |
k: int = 5
|
18 |
|
19 |
-
@
|
20 |
async def rag_query(body: QueryIn, x_api_key: str = Header(None)):
|
21 |
-
"""
|
22 |
-
Secure JSON endpoint.
|
23 |
-
Caller must send: X-API-Key: <one-of-valid-keys>
|
24 |
-
"""
|
25 |
if x_api_key not in VALID_KEYS:
|
26 |
raise HTTPException(status_code=401, detail="Invalid or missing X-API-Key")
|
|
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
# -------- 3) Public Gradio UI ----------
|
32 |
-
def run(q, k):
|
33 |
return retrieve_info(q, int(k))
|
34 |
|
35 |
demo = gr.Interface(
|
36 |
-
fn=
|
37 |
-
inputs=["text", gr.
|
38 |
outputs=gr.Textbox(lines=25, label="Retrieved chunks"),
|
|
|
39 |
allow_flagging="never",
|
40 |
-
title="Moffitt RAG Demo",
|
41 |
-
description="Type a question; we search Chroma with E5 embeddings."
|
42 |
)
|
43 |
|
44 |
-
# Mount
|
45 |
-
|
46 |
-
demo.launch()
|
47 |
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py – FastAPI core with public Gradio UI
|
2 |
+
import os
|
|
|
|
|
3 |
from fastapi import FastAPI, Header, HTTPException
|
4 |
from pydantic import BaseModel
|
5 |
+
import gradio as gr
|
6 |
+
from rag import retrieve_info # <-- function defined in rag.py
|
7 |
|
8 |
+
# =============== Config ===============
|
9 |
+
VALID_KEYS = set(os.getenv("RAG_KEYS", "alpha").split(",")) # add real keys in Space » Settings » Secrets
|
10 |
|
11 |
+
# =============== FastAPI ==============
|
12 |
+
api = FastAPI(title="Moffitt RAG API")
|
13 |
|
14 |
class QueryIn(BaseModel):
|
15 |
query: str
|
16 |
k: int = 5
|
17 |
|
18 |
+
@api.post("/v1/query")
|
19 |
async def rag_query(body: QueryIn, x_api_key: str = Header(None)):
|
|
|
|
|
|
|
|
|
20 |
if x_api_key not in VALID_KEYS:
|
21 |
raise HTTPException(status_code=401, detail="Invalid or missing X-API-Key")
|
22 |
+
answer = retrieve_info(body.query, body.k)
|
23 |
+
return {"answer": answer}
|
24 |
|
25 |
+
# =============== Public Gradio UI ==============
|
26 |
+
def ui_fn(q, k):
|
|
|
|
|
|
|
27 |
return retrieve_info(q, int(k))
|
28 |
|
29 |
demo = gr.Interface(
|
30 |
+
fn=ui_fn,
|
31 |
+
inputs=["text", gr.Slider(1, 20, value=5, step=1, label="Top-k")],
|
32 |
outputs=gr.Textbox(lines=25, label="Retrieved chunks"),
|
33 |
+
title="🔍 Moffitt Retrieval Demo",
|
34 |
allow_flagging="never",
|
|
|
|
|
35 |
)
|
36 |
|
37 |
+
# Mount UI at "/"
|
38 |
+
app = gr.mount_gradio_app(api, demo, path="/")
|
|
|
39 |
|
40 |
+
# ---------- Local debugging ----------
|
41 |
+
if __name__ == "__main__":
|
42 |
+
import uvicorn
|
43 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|