Sameer-Handsome173 commited on
Commit
655ad1d
Β·
verified Β·
1 Parent(s): 21037ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import tempfile
4
+ import os
5
+
6
+ # ==============================
7
+ # API Configuration
8
+ # ==============================
9
+ API_BASE_URL = os.getenv("API_BASE_URL", "https://sameer-handsome173-multi-modal-rag.hf.space")
10
+
11
+ ENDPOINTS = {
12
+ "ingestion": {
13
+ "ingest_pdf": f"{API_BASE_URL}/ingest",
14
+ "ingestion_stats": f"{API_BASE_URL}/ingest/stats",
15
+ },
16
+ "query": {
17
+ "query": f"{API_BASE_URL}/query",
18
+ "query_stats": f"{API_BASE_URL}/query/stats",
19
+ },
20
+ "docs": {
21
+ "swagger": f"{API_BASE_URL}/docs",
22
+ "redoc": f"{API_BASE_URL}/redoc",
23
+ }
24
+ }
25
+
26
+ TIMEOUT = 150 # seconds
27
+
28
+ # ==============================
29
+ # Helper Request Function
30
+ # ==============================
31
+ def make_request(method, url, **kwargs):
32
+ try:
33
+ response = requests.request(method, url, timeout=TIMEOUT, **kwargs)
34
+ response.raise_for_status()
35
+ return response
36
+ except requests.Timeout:
37
+ st.error("⏳ Request timed out! The server took too long to respond.")
38
+ except requests.RequestException as e:
39
+ st.error(f"⚠️ Request error: {e}")
40
+ return None
41
+
42
+ # ==============================
43
+ # Streamlit Configuration
44
+ # ==============================
45
+ st.set_page_config(page_title="Multi-Modal RAG", page_icon="πŸ€–", layout="wide")
46
+
47
+ # ==============================
48
+ # Black-Green Theme
49
+ # ==============================
50
+ st.markdown("""
51
+ <style>
52
+ body {background-color: #000000;}
53
+ .stApp {background-color: #000000; color: #00FF7F;}
54
+ h1, h2, h3, h4 {color: #00FF7F;}
55
+ .stButton>button {
56
+ background-color: #00FF7F;
57
+ color: black;
58
+ border-radius: 10px;
59
+ font-weight: bold;
60
+ }
61
+ .stButton>button:hover {
62
+ background-color: #00cc66;
63
+ color: white;
64
+ }
65
+ textarea, input {
66
+ background-color: #1a1a1a !important;
67
+ color: #00FF7F !important;
68
+ border-radius: 10px !important;
69
+ }
70
+ </style>
71
+ """, unsafe_allow_html=True)
72
+
73
+ st.title("πŸ€– Multi-Modal RAG System")
74
+ st.subheader("Upload, Ingest, and Query Seamlessly")
75
+
76
+ # ==============================
77
+ # Session State
78
+ # ==============================
79
+ if "ingested" not in st.session_state:
80
+ st.session_state.ingested = False
81
+
82
+ # ==============================
83
+ # PDF Upload and Ingestion
84
+ # ==============================
85
+ uploaded_file = st.file_uploader("πŸ“‚ Upload your PDF", type=["pdf"])
86
+
87
+ if uploaded_file:
88
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
89
+ temp_file.write(uploaded_file.read())
90
+ temp_path = temp_file.name
91
+
92
+ if st.button("πŸš€ Ingest PDF"):
93
+ with st.spinner("Uploading and ingesting your PDF... ⏳"):
94
+ with open(temp_path, "rb") as f:
95
+ files = {"file": (uploaded_file.name, f, "application/pdf")}
96
+ response = make_request("POST", ENDPOINTS["ingestion"]["ingest_pdf"], files=files)
97
+
98
+ if response and response.status_code == 200:
99
+ st.success("βœ… PDF ingested successfully!")
100
+ st.session_state.ingested = True
101
+ elif response:
102
+ st.error(f"❌ Ingestion failed: {response.text}")
103
+
104
+ # ==============================
105
+ # Query Section
106
+ # ==============================
107
+ st.markdown("---")
108
+ st.subheader("πŸ’¬ Ask a Question from the PDF")
109
+
110
+ user_query = st.text_area("Enter your query here...")
111
+
112
+ if st.button("πŸ” Ask Query"):
113
+ if not st.session_state.ingested:
114
+ st.warning("⚠️ Please ingest a PDF before querying.")
115
+ elif user_query.strip() == "":
116
+ st.warning("⚠️ Please enter a question.")
117
+ else:
118
+ with st.spinner("Querying the model... πŸ€”"):
119
+ response = make_request("POST", ENDPOINTS["query"]["query"], params={"question": user_query, "k": 5})
120
+
121
+ if response and response.status_code == 200:
122
+ result = response.json()
123
+ answer = result.get("answer", "No answer found.")
124
+ st.markdown("### 🧠 Answer:")
125
+ st.markdown(f"**{answer}**")
126
+ elif response:
127
+ st.error(f"❌ Query failed: {response.text}")