Spaces:
Running
Running
Patrick Walukagga
commited on
Commit
·
1007582
1
Parent(s):
7b9cfed
Expose api endpoints
Browse files- api.py +107 -0
- docs.py +13 -0
- requirements.txt +1 -0
- study_files.json +3 -1
api.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from gradio_client import Client
|
5 |
+
from enum import Enum
|
6 |
+
from typing import List, Optional
|
7 |
+
from pydantic import BaseModel, Field, constr, ConfigDict
|
8 |
+
from fastapi.responses import FileResponse
|
9 |
+
|
10 |
+
from docs import description, tags_metadata
|
11 |
+
|
12 |
+
|
13 |
+
app = FastAPI(
|
14 |
+
title="ACRES RAG API",
|
15 |
+
description=description,
|
16 |
+
openapi_tags=tags_metadata,
|
17 |
+
)
|
18 |
+
client = Client("http://localhost:7860/")
|
19 |
+
|
20 |
+
class StudyVariables(str, Enum):
|
21 |
+
ebola_virus = "Ebola Virus"
|
22 |
+
vaccine_coverage = "Vaccine coverage"
|
23 |
+
genexpert = "GeneXpert"
|
24 |
+
|
25 |
+
|
26 |
+
class PromptType(str, Enum):
|
27 |
+
default = "Default"
|
28 |
+
highlight = "Highlight"
|
29 |
+
evidence_based = "Evidence-based"
|
30 |
+
|
31 |
+
class StudyVariableRequest(BaseModel):
|
32 |
+
study_variable: StudyVariables
|
33 |
+
prompt_type: PromptType
|
34 |
+
text: constr(min_length=1, strip_whitespace=True) # type: ignore
|
35 |
+
|
36 |
+
model_config = ConfigDict(from_attributes=True)
|
37 |
+
|
38 |
+
class DownloadCSV(BaseModel):
|
39 |
+
text: constr(min_length=1, strip_whitespace=True) # type: ignore
|
40 |
+
|
41 |
+
model_config = ConfigDict(from_attributes=True)
|
42 |
+
|
43 |
+
|
44 |
+
class Study(BaseModel):
|
45 |
+
study_name: constr(min_length=1, strip_whitespace=True) # type: ignore
|
46 |
+
|
47 |
+
model_config = ConfigDict(from_attributes=True)
|
48 |
+
|
49 |
+
|
50 |
+
class ZoteroCredentials(BaseModel):
|
51 |
+
library_id: constr(min_length=1, strip_whitespace=True) # type: ignore
|
52 |
+
api_access_key: constr(min_length=1, strip_whitespace=True) # type: ignore
|
53 |
+
|
54 |
+
model_config = ConfigDict(from_attributes=True)
|
55 |
+
|
56 |
+
|
57 |
+
@app.post("/process_zotero_library_items", tags=["zotero"])
|
58 |
+
def process_zotero_library_items(zotero_credentials: ZoteroCredentials):
|
59 |
+
result = client.predict(
|
60 |
+
zotero_library_id=zotero_credentials.library_id,
|
61 |
+
zotero_api_access_key=zotero_credentials.api_access_key,
|
62 |
+
api_name="/process_zotero_library_items"
|
63 |
+
)
|
64 |
+
return {"result":result}
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
@app.post("/get_study_info", tags=["zotero"])
|
69 |
+
def get_study_info(study: Study):
|
70 |
+
result = client.predict(
|
71 |
+
study_name=study.study_name,
|
72 |
+
api_name="/get_study_info"
|
73 |
+
)
|
74 |
+
# print(result)
|
75 |
+
return {"result":result}
|
76 |
+
|
77 |
+
|
78 |
+
@app.post("/study_variables", tags=["zotero"])
|
79 |
+
def process_study_variables(study_request: StudyVariableRequest,):
|
80 |
+
result = client.predict(
|
81 |
+
text=study_request.text, # "study id, study title, study design, study summary",
|
82 |
+
study_name=study_request.study_variable, # "Ebola Virus",
|
83 |
+
prompt_type=study_request.prompt_type, #"Default",
|
84 |
+
api_name="/process_multi_input"
|
85 |
+
)
|
86 |
+
print(type(result))
|
87 |
+
return {"result":result[0]}
|
88 |
+
|
89 |
+
|
90 |
+
@app.post("/download_csv", tags=["zotero"])
|
91 |
+
def download_csv(download_request: DownloadCSV):
|
92 |
+
result = client.predict(
|
93 |
+
markdown_content=download_request.text,
|
94 |
+
api_name="/download_as_csv"
|
95 |
+
)
|
96 |
+
print(result)
|
97 |
+
|
98 |
+
file_path = result
|
99 |
+
if not file_path or not os.path.exists(file_path):
|
100 |
+
raise HTTPException(status_code=404, detail="File not found")
|
101 |
+
|
102 |
+
# Use FileResponse to send the file to the client
|
103 |
+
return FileResponse(
|
104 |
+
file_path,
|
105 |
+
media_type="text/csv", # Specify the correct MIME type for CSV
|
106 |
+
filename=os.path.basename(file_path) # Provide a default filename for the download
|
107 |
+
)
|
docs.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
description = """
|
2 |
+
Welcome to the Acres AI RAG API documentation.
|
3 |
+
|
4 |
+
### RAG Tasks
|
5 |
+
- Use the `/process_zotero_library_items`: Process zotero library items with your zotero credentials.
|
6 |
+
- Use the `/get_study_info`: Get number of documents in a zotero study.
|
7 |
+
- Use the `/study_variables`: Get research summary from the study provided the study variables.
|
8 |
+
- Use the `/download_csv`: Export the markdown text to a csv file.
|
9 |
+
"""
|
10 |
+
|
11 |
+
tags_metadata = [
|
12 |
+
{"name": "ACRES RAG", "description": "AI RAG Application"},
|
13 |
+
]
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
chromadb==0.5.5
|
2 |
fastapi==0.112.2
|
3 |
gradio
|
|
|
4 |
llama-index
|
5 |
llama-index-vector-stores-chroma
|
6 |
nest-asyncio==1.6.0
|
|
|
1 |
chromadb==0.5.5
|
2 |
fastapi==0.112.2
|
3 |
gradio
|
4 |
+
gradio_client
|
5 |
llama-index
|
6 |
llama-index-vector-stores-chroma
|
7 |
nest-asyncio==1.6.0
|
study_files.json
CHANGED
@@ -4,5 +4,7 @@
|
|
4 |
"GeneXpert": "data/gene_xpert_zotero_items.json",
|
5 |
"Zotero Collection Pastan": "data/zotero-collection-pastan_zotero_items.json",
|
6 |
"EBSCOhost": "data/ebscohost_zotero_items.json",
|
7 |
-
"ExportedRis_file_1_of_1 (1)": "data/exportedris-file-1-of-1-1_zotero_items.json"
|
|
|
|
|
8 |
}
|
|
|
4 |
"GeneXpert": "data/gene_xpert_zotero_items.json",
|
5 |
"Zotero Collection Pastan": "data/zotero-collection-pastan_zotero_items.json",
|
6 |
"EBSCOhost": "data/ebscohost_zotero_items.json",
|
7 |
+
"ExportedRis_file_1_of_1 (1)": "data/exportedris-file-1-of-1-1_zotero_items.json",
|
8 |
+
"wb_1813-9450-6689": "data/wb-1813-9450-6689_zotero_items.json",
|
9 |
+
"kayongo papers": "data/kayongo-papers_zotero_items.json"
|
10 |
}
|