Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,83 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
from qdrant_client import QdrantClient
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
-
|
| 6 |
-
# Qdrant details
|
| 7 |
-
QDRANT_URL = "https://807708a6-1d41-4ecb-a1f3-8a41fcd48ec3.us-east4-0.gcp.cloud.qdrant.io:6333"
|
| 8 |
-
QDRANT_API_KEY = "J3LJcoG3q_njIvu9OzjooR2VBD-tx_Zz553gGwMoUD_xzdYz1tFufA"
|
| 9 |
-
QDRANT_COLLECTION_NAME = "courses-data"
|
| 10 |
-
|
| 11 |
-
# Google Gemini API details
|
| 12 |
-
GEMINI_API_KEY = "API KEY"
|
| 13 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
-
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 15 |
-
|
| 16 |
-
# Initialize Qdrant client
|
| 17 |
-
qdrant_client = QdrantClient(url=QDRANT_URL, prefer_grpc=
|
| 18 |
-
|
| 19 |
-
# Load the SentenceTransformer model
|
| 20 |
-
embedder = SentenceTransformer('all-MiniLM-L6-v2') # Vector size = 384
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def vector_search(query, collection_name, top_k):
|
| 24 |
-
"""Perform a vector search on the Qdrant collection."""
|
| 25 |
-
query_vector = embedder.encode(query).tolist()
|
| 26 |
-
search_result = qdrant_client.search(
|
| 27 |
-
collection_name=collection_name,
|
| 28 |
-
query_vector=query_vector,
|
| 29 |
-
limit=top_k
|
| 30 |
-
)
|
| 31 |
-
results = []
|
| 32 |
-
for result in search_result:
|
| 33 |
-
chunk_text = result.payload.get('page_content', 'No text found')
|
| 34 |
-
results.append(chunk_text)
|
| 35 |
-
return results
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def gemini(query, chunks):
|
| 39 |
-
"""Generates an answer using Google's Generative AI (Gemini)."""
|
| 40 |
-
context = "\n".join([f"{i+1}. {chunk}" for i, chunk in enumerate(chunks)])
|
| 41 |
-
prompt = f"""
|
| 42 |
-
You are a highly knowledgeable assistant. Based on the given context, please provide a well-crafted answer to the query below. Use the provided information from the context as reference material.
|
| 43 |
-
|
| 44 |
-
### Context:
|
| 45 |
-
{context}
|
| 46 |
-
|
| 47 |
-
### Query:
|
| 48 |
-
{query}
|
| 49 |
-
|
| 50 |
-
Based on the context, provide a list of courses - course names and a short description.
|
| 51 |
-
Provide a concise, clear, and informative response based on the query.
|
| 52 |
-
"""
|
| 53 |
-
# Make the request to generate text
|
| 54 |
-
response = model.generate_content(prompt)
|
| 55 |
-
|
| 56 |
-
# Check if the response contains valid content
|
| 57 |
-
if response.candidates and len(response.candidates) > 0:
|
| 58 |
-
return response.text # Return the generated text as a string
|
| 59 |
-
else:
|
| 60 |
-
return "No valid content was returned. Please adjust your prompt or try again."
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
def getResult(input_query):
|
| 64 |
-
context = vector_search(input_query, QDRANT_COLLECTION_NAME, top_k=5)
|
| 65 |
-
return gemini(input_query, context)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# Streamlit App
|
| 69 |
-
st.title("Course Finder using RAG System")
|
| 70 |
-
st.write("Search for courses using a query. The system retrieves and generates relevant course details.")
|
| 71 |
-
|
| 72 |
-
# Search bar for user input
|
| 73 |
-
query = st.text_input("Enter your query:", "")
|
| 74 |
-
|
| 75 |
-
# Display the result when the user enters a query
|
| 76 |
-
if st.button("Search"):
|
| 77 |
-
if query.strip():
|
| 78 |
-
with st.spinner("Searching and generating results..."):
|
| 79 |
-
result = getResult(query)
|
| 80 |
-
st.subheader("Results:")
|
| 81 |
-
st.write(result)
|
| 82 |
-
else:
|
| 83 |
-
st.warning("Please enter a valid query!")
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from qdrant_client import QdrantClient
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
|
| 6 |
+
# Qdrant details
|
| 7 |
+
QDRANT_URL = "https://807708a6-1d41-4ecb-a1f3-8a41fcd48ec3.us-east4-0.gcp.cloud.qdrant.io:6333"
|
| 8 |
+
QDRANT_API_KEY = "J3LJcoG3q_njIvu9OzjooR2VBD-tx_Zz553gGwMoUD_xzdYz1tFufA"
|
| 9 |
+
QDRANT_COLLECTION_NAME = "courses-data"
|
| 10 |
+
|
| 11 |
+
# Google Gemini API details
|
| 12 |
+
GEMINI_API_KEY = "API KEY"
|
| 13 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 15 |
+
|
| 16 |
+
# Initialize Qdrant client
|
| 17 |
+
qdrant_client = QdrantClient(url=QDRANT_URL, prefer_grpc=False, api_key=QDRANT_API_KEY)
|
| 18 |
+
|
| 19 |
+
# Load the SentenceTransformer model
|
| 20 |
+
embedder = SentenceTransformer('all-MiniLM-L6-v2') # Vector size = 384
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def vector_search(query, collection_name, top_k):
|
| 24 |
+
"""Perform a vector search on the Qdrant collection."""
|
| 25 |
+
query_vector = embedder.encode(query).tolist()
|
| 26 |
+
search_result = qdrant_client.search(
|
| 27 |
+
collection_name=collection_name,
|
| 28 |
+
query_vector=query_vector,
|
| 29 |
+
limit=top_k
|
| 30 |
+
)
|
| 31 |
+
results = []
|
| 32 |
+
for result in search_result:
|
| 33 |
+
chunk_text = result.payload.get('page_content', 'No text found')
|
| 34 |
+
results.append(chunk_text)
|
| 35 |
+
return results
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def gemini(query, chunks):
|
| 39 |
+
"""Generates an answer using Google's Generative AI (Gemini)."""
|
| 40 |
+
context = "\n".join([f"{i+1}. {chunk}" for i, chunk in enumerate(chunks)])
|
| 41 |
+
prompt = f"""
|
| 42 |
+
You are a highly knowledgeable assistant. Based on the given context, please provide a well-crafted answer to the query below. Use the provided information from the context as reference material.
|
| 43 |
+
|
| 44 |
+
### Context:
|
| 45 |
+
{context}
|
| 46 |
+
|
| 47 |
+
### Query:
|
| 48 |
+
{query}
|
| 49 |
+
|
| 50 |
+
Based on the context, provide a list of courses - course names and a short description.
|
| 51 |
+
Provide a concise, clear, and informative response based on the query.
|
| 52 |
+
"""
|
| 53 |
+
# Make the request to generate text
|
| 54 |
+
response = model.generate_content(prompt)
|
| 55 |
+
|
| 56 |
+
# Check if the response contains valid content
|
| 57 |
+
if response.candidates and len(response.candidates) > 0:
|
| 58 |
+
return response.text # Return the generated text as a string
|
| 59 |
+
else:
|
| 60 |
+
return "No valid content was returned. Please adjust your prompt or try again."
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def getResult(input_query):
|
| 64 |
+
context = vector_search(input_query, QDRANT_COLLECTION_NAME, top_k=5)
|
| 65 |
+
return gemini(input_query, context)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# Streamlit App
|
| 69 |
+
st.title("Course Finder using RAG System")
|
| 70 |
+
st.write("Search for courses using a query. The system retrieves and generates relevant course details.")
|
| 71 |
+
|
| 72 |
+
# Search bar for user input
|
| 73 |
+
query = st.text_input("Enter your query:", "")
|
| 74 |
+
|
| 75 |
+
# Display the result when the user enters a query
|
| 76 |
+
if st.button("Search"):
|
| 77 |
+
if query.strip():
|
| 78 |
+
with st.spinner("Searching and generating results..."):
|
| 79 |
+
result = getResult(query)
|
| 80 |
+
st.subheader("Results:")
|
| 81 |
+
st.write(result)
|
| 82 |
+
else:
|
| 83 |
+
st.warning("Please enter a valid query!")
|