meraj12 commited on
Commit
77b1c2d
·
verified ·
1 Parent(s): 598233a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -1,19 +1,40 @@
1
- # Save this as app.py
2
  import streamlit as st
3
  import fitz # PyMuPDF
 
4
  import os
 
 
5
  import requests
6
 
7
- # Load PDF content
8
- def load_pdf_text(pdf_path):
 
 
 
9
  doc = fitz.open(pdf_path)
10
- text = ""
11
  for page in doc:
12
- text += page.get_text()
13
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Ask LLM using Groq API
16
- def ask_llm_groq(question, context, groq_api_key):
17
  headers = {
18
  "Authorization": f"Bearer {groq_api_key}",
19
  "Content-Type": "application/json"
@@ -22,37 +43,35 @@ def ask_llm_groq(question, context, groq_api_key):
22
  data = {
23
  "model": "llama3-8b-8192",
24
  "messages": [
25
- {"role": "system", "content": "Answer based on the provided context."},
26
- {"role": "user", "content": f"Context: {context}\n\nQuestion: {question}"}
27
  ]
28
  }
29
 
30
  response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
31
- if response.status_code == 200:
32
- return response.json()['choices'][0]['message']['content']
33
- else:
34
- return "Error: " + response.text
35
 
36
  # Streamlit UI
37
- st.set_page_config(page_title="Meraj Info Assistant", layout="centered")
38
- st.title("📄 Meraj Graphics RAG Chatbot")
39
 
40
- uploaded_pdf = st.file_uploader("Upload the Info PDF", type="pdf")
41
 
42
  if uploaded_pdf:
43
  with open("temp.pdf", "wb") as f:
44
  f.write(uploaded_pdf.read())
45
- context = load_pdf_text("temp.pdf")
46
 
47
  st.success("PDF uploaded successfully!")
 
 
48
 
49
- question = st.text_input("Ask anything about Meraj Graphics:")
50
- groq_api_key = st.text_input("Enter your Groq API key", type="password")
51
 
52
- if st.button("Ask"):
53
- if question and groq_api_key:
54
- with st.spinner("Thinking..."):
55
- answer = ask_llm_groq(question, context, groq_api_key)
56
- st.markdown(f"**Answer:** {answer}")
57
  else:
58
- st.warning("Please enter both question and API key.")
 
1
+ # Save as app.py
2
  import streamlit as st
3
  import fitz # PyMuPDF
4
+ import faiss
5
  import os
6
+ import numpy as np
7
+ from sentence_transformers import SentenceTransformer
8
  import requests
9
 
10
+ # Load SentenceTransformer model
11
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
12
+
13
+ # Load and split PDF
14
+ def load_and_split_pdf(pdf_path):
15
  doc = fitz.open(pdf_path)
16
+ chunks = []
17
  for page in doc:
18
+ text = page.get_text()
19
+ chunks.extend([chunk.strip() for chunk in text.split("\n") if chunk.strip()])
20
+ return chunks
21
+
22
+ # Embed chunks and save in FAISS
23
+ def create_faiss_index(text_chunks):
24
+ embeddings = embedder.encode(text_chunks)
25
+ index = faiss.IndexFlatL2(embeddings.shape[1])
26
+ index.add(np.array(embeddings))
27
+ return index, text_chunks, embeddings
28
+
29
+ # Search in FAISS
30
+ def search_faiss(query, index, text_chunks, k=3):
31
+ query_vec = embedder.encode([query])
32
+ D, I = index.search(query_vec, k)
33
+ results = [text_chunks[i] for i in I[0]]
34
+ return "\n".join(results)
35
 
36
+ # Call Groq API with context
37
+ def ask_groq(query, context, groq_api_key):
38
  headers = {
39
  "Authorization": f"Bearer {groq_api_key}",
40
  "Content-Type": "application/json"
 
43
  data = {
44
  "model": "llama3-8b-8192",
45
  "messages": [
46
+ {"role": "system", "content": "Answer the user's question using the provided context."},
47
+ {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {query}"}
48
  ]
49
  }
50
 
51
  response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data)
52
+ return response.json()['choices'][0]['message']['content'] if response.status_code == 200 else response.text
 
 
 
53
 
54
  # Streamlit UI
55
+ st.set_page_config(page_title="Meraj Graphics RAG Bot")
56
+ st.title("📄 Meraj Graphics Assistant")
57
 
58
+ uploaded_pdf = st.file_uploader("Upload your PDF with business info", type="pdf")
59
 
60
  if uploaded_pdf:
61
  with open("temp.pdf", "wb") as f:
62
  f.write(uploaded_pdf.read())
 
63
 
64
  st.success("PDF uploaded successfully!")
65
+ chunks = load_and_split_pdf("temp.pdf")
66
+ index, chunk_texts, embeddings = create_faiss_index(chunks)
67
 
68
+ query = st.text_input("Ask a question about our services:")
69
+ groq_key = st.text_input("Enter your Groq API key", type="password")
70
 
71
+ if st.button("Get Answer"):
72
+ if query and groq_key:
73
+ context = search_faiss(query, index, chunk_texts)
74
+ answer = ask_groq(query, context, groq_key)
75
+ st.markdown(f"**Answer:** {answer}")
76
  else:
77
+ st.warning("Please enter both a query and API key.")