Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,188 +1,147 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
from groq import Groq
|
|
|
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
import requests
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ---------------------------
|
| 8 |
# PAGE CONFIG
|
| 9 |
# ---------------------------
|
| 10 |
-
st.set_page_config(page_title="Krish GPT
|
| 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 |
if "messages" not in st.session_state:
|
| 98 |
st.session_state.messages = []
|
| 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 |
-
|
| 129 |
-
|
| 130 |
-
)
|
| 131 |
-
with col2:
|
| 132 |
-
uploaded_file = st.file_uploader("", label_visibility="collapsed")
|
| 133 |
-
with col3:
|
| 134 |
-
submitted = st.form_submit_button("Send")
|
| 135 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 136 |
-
|
| 137 |
-
if submitted and (prompt.strip() != "" or uploaded_file):
|
| 138 |
-
context = ""
|
| 139 |
-
if uploaded_file:
|
| 140 |
-
if uploaded_file.type == "application/pdf":
|
| 141 |
-
reader = PdfReader(uploaded_file)
|
| 142 |
-
for page in reader.pages:
|
| 143 |
-
text = page.extract_text()
|
| 144 |
-
if text:
|
| 145 |
-
context += text
|
| 146 |
-
else:
|
| 147 |
-
try:
|
| 148 |
-
res = requests.post(
|
| 149 |
-
"https://api.ocr.space/parse/image",
|
| 150 |
-
files={"file": uploaded_file},
|
| 151 |
-
data={"apikey": OCR_API_KEY}
|
| 152 |
-
)
|
| 153 |
-
context = res.json()['ParsedResults'][0]['ParsedText']
|
| 154 |
-
except:
|
| 155 |
-
context = ""
|
| 156 |
-
|
| 157 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 158 |
-
|
| 159 |
-
final_prompt = (prompt or "") + "\n" + context[:2000]
|
| 160 |
-
|
| 161 |
-
with st.spinner("🤖 Thinking..."):
|
| 162 |
-
try:
|
| 163 |
-
response = client.chat.completions.create(
|
| 164 |
-
model="llama-3.3-70b-versatile",
|
| 165 |
-
messages=[{"role": "user", "content": final_prompt}]
|
| 166 |
-
)
|
| 167 |
-
reply = response.choices[0].message.content
|
| 168 |
-
except:
|
| 169 |
-
reply = "⚠️ Something went wrong. Try again."
|
| 170 |
-
|
| 171 |
-
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 172 |
-
|
| 173 |
-
# ---------------------------
|
| 174 |
-
# JS Trick: Enter = Send, Ctrl+Enter = New line
|
| 175 |
-
# ---------------------------
|
| 176 |
-
st.markdown("""
|
| 177 |
-
<script>
|
| 178 |
-
const textarea = window.parent.document.querySelector('textarea');
|
| 179 |
-
if (textarea) {
|
| 180 |
-
textarea.addEventListener('keydown', function(e){
|
| 181 |
-
if(e.key === 'Enter' && !e.ctrlKey){
|
| 182 |
-
e.preventDefault();
|
| 183 |
-
document.querySelector('button[kind="primary"]').click();
|
| 184 |
-
}
|
| 185 |
-
});
|
| 186 |
-
}
|
| 187 |
-
</script>
|
| 188 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
import requests
|
| 6 |
+
import numpy as np
|
| 7 |
+
import faiss
|
| 8 |
+
from sentence_transformers import SentenceTransformer
|
| 9 |
|
| 10 |
# ---------------------------
|
| 11 |
# PAGE CONFIG
|
| 12 |
# ---------------------------
|
| 13 |
+
st.set_page_config(page_title="Krish GPT Multi-Modal RAG", layout="wide")
|
| 14 |
+
st.title("🤖 Krish GPT Multi-Modal RAG")
|
| 15 |
+
st.caption("PDF + Image OCR + RAG using Groq LLM 🚀")
|
| 16 |
+
|
| 17 |
+
# ---------------------------
|
| 18 |
+
# API KEYS
|
| 19 |
+
# ---------------------------
|
| 20 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 21 |
+
ocr_api_key = os.getenv("OCR_API_KEY")
|
| 22 |
+
|
| 23 |
+
if not groq_api_key:
|
| 24 |
+
groq_api_key = st.text_input("Enter GROQ API Key", type="password")
|
| 25 |
+
|
| 26 |
+
if not ocr_api_key:
|
| 27 |
+
ocr_api_key = st.text_input("Enter OCR.Space API Key", type="password")
|
| 28 |
+
|
| 29 |
+
if not groq_api_key or not ocr_api_key:
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
client = Groq(api_key=groq_api_key)
|
| 33 |
+
|
| 34 |
+
# ---------------------------
|
| 35 |
+
# EMBEDDING MODEL
|
| 36 |
+
# ---------------------------
|
| 37 |
+
@st.cache_resource
|
| 38 |
+
def load_embedder():
|
| 39 |
+
return SentenceTransformer("all-MiniLM-L6-v2")
|
| 40 |
+
|
| 41 |
+
embedder = load_embedder()
|
| 42 |
+
|
| 43 |
+
# ---------------------------
|
| 44 |
+
# OCR Function
|
| 45 |
+
# ---------------------------
|
| 46 |
+
def ocr_space_image(file, api_key):
|
| 47 |
+
url = "https://api.ocr.space/parse/image"
|
| 48 |
+
files = {'file': file}
|
| 49 |
+
data = {'apikey': api_key, 'language': 'eng'}
|
| 50 |
+
r = requests.post(url, files=files, data=data)
|
| 51 |
+
try:
|
| 52 |
+
result = r.json()
|
| 53 |
+
text = result['ParsedResults'][0]['ParsedText']
|
| 54 |
+
except:
|
| 55 |
+
text = ""
|
| 56 |
+
return text
|
| 57 |
+
|
| 58 |
+
# ---------------------------
|
| 59 |
+
# FILE UPLOAD
|
| 60 |
+
# ---------------------------
|
| 61 |
+
uploaded_file = st.file_uploader(
|
| 62 |
+
"Upload PDF or Image", type=["pdf", "png", "jpg", "jpeg"]
|
| 63 |
+
)
|
| 64 |
+
file_text = ""
|
| 65 |
+
|
| 66 |
+
if uploaded_file:
|
| 67 |
+
if uploaded_file.type == "application/pdf":
|
| 68 |
+
reader = PdfReader(uploaded_file)
|
| 69 |
+
for page in reader.pages:
|
| 70 |
+
t = page.extract_text()
|
| 71 |
+
if t:
|
| 72 |
+
file_text += t
|
| 73 |
+
elif "image" in uploaded_file.type:
|
| 74 |
+
file_text = ocr_space_image(uploaded_file, ocr_api_key)
|
| 75 |
+
|
| 76 |
+
# ---------------------------
|
| 77 |
+
# TEXT CHUNKING & FAISS
|
| 78 |
+
# ---------------------------
|
| 79 |
+
def chunk_text(text, chunk_size=500):
|
| 80 |
+
chunks = []
|
| 81 |
+
for i in range(0, len(text), chunk_size):
|
| 82 |
+
chunks.append(text[i:i+chunk_size])
|
| 83 |
+
return chunks
|
| 84 |
+
|
| 85 |
+
def build_index(chunks):
|
| 86 |
+
embeddings = embedder.encode(chunks)
|
| 87 |
+
dim = embeddings.shape[1]
|
| 88 |
+
index = faiss.IndexFlatL2(dim)
|
| 89 |
+
index.add(np.array(embeddings))
|
| 90 |
+
return index, embeddings
|
| 91 |
+
|
| 92 |
+
def search(query, chunks, index):
|
| 93 |
+
q_emb = embedder.encode([query])
|
| 94 |
+
D, I = index.search(np.array(q_emb), k=min(3, len(chunks)))
|
| 95 |
+
results = [chunks[i] for i in I[0]]
|
| 96 |
+
return "\n".join(results)
|
| 97 |
+
|
| 98 |
+
# ---------------------------
|
| 99 |
+
# PROCESS FILE
|
| 100 |
+
# ---------------------------
|
| 101 |
+
if uploaded_file and file_text:
|
| 102 |
+
chunks = chunk_text(file_text)
|
| 103 |
+
index, embeddings = build_index(chunks)
|
| 104 |
+
st.session_state.rag_data = (chunks, index)
|
| 105 |
+
|
| 106 |
+
# ---------------------------
|
| 107 |
+
# CHAT MEMORY
|
| 108 |
# ---------------------------
|
| 109 |
if "messages" not in st.session_state:
|
| 110 |
st.session_state.messages = []
|
| 111 |
|
| 112 |
+
for msg in st.session_state.messages:
|
| 113 |
+
with st.chat_message(msg["role"]):
|
| 114 |
+
st.markdown(msg["content"])
|
| 115 |
+
|
| 116 |
+
# ---------------------------
|
| 117 |
+
# USER PROMPT
|
| 118 |
# ---------------------------
|
| 119 |
+
prompt = st.chat_input("Ask anything...")
|
| 120 |
+
|
| 121 |
+
if prompt:
|
| 122 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 123 |
+
with st.chat_message("user"):
|
| 124 |
+
st.markdown(prompt)
|
| 125 |
+
|
| 126 |
+
context = ""
|
| 127 |
+
if "rag_data" in st.session_state:
|
| 128 |
+
chunks, index = st.session_state.rag_data
|
| 129 |
+
context = search(prompt, chunks, index)
|
| 130 |
+
|
| 131 |
+
with st.chat_message("assistant"):
|
| 132 |
+
try:
|
| 133 |
+
response = client.chat.completions.create(
|
| 134 |
+
model="llama-3.3-70b-versatile",
|
| 135 |
+
messages=[
|
| 136 |
+
{"role": "system", "content": f"Context:\n{context}"},
|
| 137 |
+
*st.session_state.messages
|
| 138 |
+
],
|
| 139 |
+
temperature=0.7,
|
| 140 |
+
max_tokens=1024
|
| 141 |
+
)
|
| 142 |
+
reply = response.choices[0].message.content
|
| 143 |
+
except Exception as e:
|
| 144 |
+
reply = f"❌ Error: {str(e)}"
|
| 145 |
+
|
| 146 |
+
st.markdown(reply)
|
| 147 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|