File size: 4,206 Bytes
655ad1d |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
import streamlit as st
import requests
import tempfile
import os
# ==============================
# API Configuration
# ==============================
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 # seconds
# ==============================
# Helper Request Function
# ==============================
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
# ==============================
# Streamlit Configuration
# ==============================
st.set_page_config(page_title="Multi-Modal RAG", page_icon="π€", layout="wide")
# ==============================
# Black-Green Theme
# ==============================
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")
# ==============================
# Session State
# ==============================
if "ingested" not in st.session_state:
st.session_state.ingested = False
# ==============================
# PDF Upload and Ingestion
# ==============================
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}")
# ==============================
# Query Section
# ==============================
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}")
|