Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,58 @@
|
|
| 1 |
-
#
|
| 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 |
-
|
| 11 |
-
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
|
| 13 |
-
# Load and
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return chunks
|
| 21 |
|
| 22 |
-
|
| 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
|
| 30 |
-
def
|
| 31 |
-
|
| 32 |
-
D, I = index.search(
|
| 33 |
-
results = [
|
| 34 |
return "\n".join(results)
|
| 35 |
|
| 36 |
-
# Call Groq API
|
| 37 |
-
def
|
|
|
|
| 38 |
headers = {
|
| 39 |
-
"Authorization": f"Bearer {
|
| 40 |
"Content-Type": "application/json"
|
| 41 |
}
|
| 42 |
-
|
| 43 |
data = {
|
| 44 |
"model": "llama3-8b-8192",
|
| 45 |
"messages": [
|
| 46 |
-
{"role": "system", "content": "Answer
|
| 47 |
-
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {
|
| 48 |
]
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
response
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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.")
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import faiss
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import requests
|
| 7 |
|
| 8 |
+
st.set_page_config(page_title="Meraj Graphics Assistant")
|
|
|
|
| 9 |
|
| 10 |
+
# Load model and index
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_data():
|
| 13 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 14 |
+
index = faiss.read_index("index.faiss")
|
| 15 |
+
chunks = np.load("chunks.npy", allow_pickle=True)
|
| 16 |
+
return embedder, index, chunks
|
|
|
|
| 17 |
|
| 18 |
+
embedder, index, chunks = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Search FAISS
|
| 21 |
+
def search(query, top_k=3):
|
| 22 |
+
q_embed = embedder.encode([query])
|
| 23 |
+
D, I = index.search(np.array(q_embed), top_k)
|
| 24 |
+
results = [chunks[i] for i in I[0]]
|
| 25 |
return "\n".join(results)
|
| 26 |
|
| 27 |
+
# Call Groq API
|
| 28 |
+
def query_groq(context, question, api_key):
|
| 29 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 30 |
headers = {
|
| 31 |
+
"Authorization": f"Bearer {api_key}",
|
| 32 |
"Content-Type": "application/json"
|
| 33 |
}
|
|
|
|
| 34 |
data = {
|
| 35 |
"model": "llama3-8b-8192",
|
| 36 |
"messages": [
|
| 37 |
+
{"role": "system", "content": "Answer based on the context."},
|
| 38 |
+
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"}
|
| 39 |
]
|
| 40 |
}
|
| 41 |
+
response = requests.post(url, headers=headers, json=data)
|
| 42 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 43 |
+
|
| 44 |
+
# UI
|
| 45 |
+
st.title("📋 Meraj Graphics Chat Assistant")
|
| 46 |
+
|
| 47 |
+
question = st.text_input("Ask something about our services:")
|
| 48 |
+
groq_key = st.text_input("Groq API Key", type="password")
|
| 49 |
+
|
| 50 |
+
if st.button("Get Answer"):
|
| 51 |
+
if not question or not groq_key:
|
| 52 |
+
st.warning("Please provide both question and API key.")
|
| 53 |
+
else:
|
| 54 |
+
with st.spinner("Searching..."):
|
| 55 |
+
context = search(question)
|
| 56 |
+
answer = query_groq(context, question, groq_key)
|
| 57 |
+
st.success("Answer:")
|
| 58 |
+
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|