|
|
import streamlit as st |
|
|
import requests |
|
|
import tempfile |
|
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API_BASE_URL = os.getenv("API_BASE_URL", "https://sameer-handsome173-multi-modal-rag.hf.space") |
|
|
|
|
|
ENDPOINTS = { |
|
|
"ingestion": { |
|
|
"ingest_pdf": f"{API_BASE_URL}/ingest", |
|
|
"ingestion_stats": f"{API_BASE_URL}/ingest/stats", |
|
|
}, |
|
|
"query": { |
|
|
"query": f"{API_BASE_URL}/query", |
|
|
"query_stats": f"{API_BASE_URL}/query/stats", |
|
|
}, |
|
|
"docs": { |
|
|
"swagger": f"{API_BASE_URL}/docs", |
|
|
"redoc": f"{API_BASE_URL}/redoc", |
|
|
} |
|
|
} |
|
|
|
|
|
TIMEOUT = 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_request(method, url, **kwargs): |
|
|
try: |
|
|
response = requests.request(method, url, timeout=TIMEOUT, **kwargs) |
|
|
response.raise_for_status() |
|
|
return response |
|
|
except requests.Timeout: |
|
|
st.error("β³ Request timed out! The server took too long to respond.") |
|
|
except requests.RequestException as e: |
|
|
st.error(f"β οΈ Request error: {e}") |
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Multi-Modal RAG", page_icon="π€", layout="wide") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(""" |
|
|
<style> |
|
|
body {background-color: #000000;} |
|
|
.stApp {background-color: #000000; color: #00FF7F;} |
|
|
h1, h2, h3, h4 {color: #00FF7F;} |
|
|
.stButton>button { |
|
|
background-color: #00FF7F; |
|
|
color: black; |
|
|
border-radius: 10px; |
|
|
font-weight: bold; |
|
|
} |
|
|
.stButton>button:hover { |
|
|
background-color: #00cc66; |
|
|
color: white; |
|
|
} |
|
|
textarea, input { |
|
|
background-color: #1a1a1a !important; |
|
|
color: #00FF7F !important; |
|
|
border-radius: 10px !important; |
|
|
} |
|
|
</style> |
|
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("π€ Multi-Modal RAG System") |
|
|
st.subheader("Upload, Ingest, and Query Seamlessly") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if "ingested" not in st.session_state: |
|
|
st.session_state.ingested = False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader("π Upload your PDF", type=["pdf"]) |
|
|
|
|
|
if uploaded_file: |
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file: |
|
|
temp_file.write(uploaded_file.read()) |
|
|
temp_path = temp_file.name |
|
|
|
|
|
if st.button("π Ingest PDF"): |
|
|
with st.spinner("Uploading and ingesting your PDF... β³"): |
|
|
with open(temp_path, "rb") as f: |
|
|
files = {"file": (uploaded_file.name, f, "application/pdf")} |
|
|
response = make_request("POST", ENDPOINTS["ingestion"]["ingest_pdf"], files=files) |
|
|
|
|
|
if response and response.status_code == 200: |
|
|
st.success("β
PDF ingested successfully!") |
|
|
st.session_state.ingested = True |
|
|
elif response: |
|
|
st.error(f"β Ingestion failed: {response.text}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown("---") |
|
|
st.subheader("π¬ Ask a Question from the PDF") |
|
|
|
|
|
user_query = st.text_area("Enter your query here...") |
|
|
|
|
|
if st.button("π Ask Query"): |
|
|
if not st.session_state.ingested: |
|
|
st.warning("β οΈ Please ingest a PDF before querying.") |
|
|
elif user_query.strip() == "": |
|
|
st.warning("β οΈ Please enter a question.") |
|
|
else: |
|
|
with st.spinner("Querying the model... π€"): |
|
|
response = make_request("POST", ENDPOINTS["query"]["query"], params={"question": user_query, "k": 5}) |
|
|
|
|
|
if response and response.status_code == 200: |
|
|
result = response.json() |
|
|
answer = result.get("answer", "No answer found.") |
|
|
st.markdown("### π§ Answer:") |
|
|
st.markdown(f"**{answer}**") |
|
|
elif response: |
|
|
st.error(f"β Query failed: {response.text}") |
|
|
|