Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load the
|
| 6 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
|
| 8 |
-
#
|
| 9 |
faq_data = [
|
| 10 |
("What is Hugging Face?", "Hugging Face is a company specializing in AI and machine learning, known for their open-source models and datasets."),
|
| 11 |
-
("
|
| 12 |
-
|
| 13 |
-
("What is a transformer model?", "A transformer model is a deep learning model that uses attention mechanisms to process and generate sequences of data, such as text or speech."),
|
| 14 |
-
("Can I use Hugging Face models in production?", "Yes, Hugging Face provides tools and frameworks like `transformers` for deploying models into production environments."),
|
| 15 |
-
("What is RAG?", "Retrieval-Augmented Generation (RAG) combines pre-trained models with retrieval systems to answer questions using both the knowledge from the model and external documents."),
|
| 16 |
-
("What is AI?", "Artificial Intelligence (AI) is the simulation of human intelligence in machines, enabling them to perform tasks that typically require human cognition."),
|
| 17 |
-
("Tell me a joke", "Why don't skeletons fight each other? They don’t have the guts!"),
|
| 18 |
-
("What is the capital of France?", "The capital of France is Paris."),
|
| 19 |
-
("How can I contact support?", "You can contact support via our website or email for assistance."),
|
| 20 |
-
("What's the weather like today?", "Sorry, I don't have access to real-time data, but I suggest checking a weather app for the latest updates.")
|
| 21 |
]
|
| 22 |
|
| 23 |
-
# Encode the FAQ dataset
|
| 24 |
corpus = [item[0] for item in faq_data] # Questions only
|
| 25 |
-
answers = {item[0]: item[1] for item in faq_data} # Map questions to
|
| 26 |
corpus_embeddings = model.encode(corpus, convert_to_tensor=True)
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def retrieve(query):
|
| 30 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Launch the Gradio interface
|
| 46 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from sentence_transformers import SentenceTransformer, util
|
| 3 |
import torch
|
| 4 |
+
import faiss
|
| 5 |
+
import chromadb
|
| 6 |
|
| 7 |
+
# Load the SentenceTransformer model for vector embeddings
|
| 8 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
|
| 10 |
+
# FAQ dataset (this can be expanded)
|
| 11 |
faq_data = [
|
| 12 |
("What is Hugging Face?", "Hugging Face is a company specializing in AI and machine learning, known for their open-source models and datasets."),
|
| 13 |
+
("What is AI?", "Artificial Intelligence (AI) is the simulation of human intelligence in machines.")
|
| 14 |
+
# Add more FAQ pairs...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
]
|
| 16 |
|
|
|
|
| 17 |
corpus = [item[0] for item in faq_data] # Questions only
|
| 18 |
+
answers = {item[0]: item[1] for item in faq_data} # Map questions to answers
|
| 19 |
corpus_embeddings = model.encode(corpus, convert_to_tensor=True)
|
| 20 |
|
| 21 |
+
# Initialize FAISS Index
|
| 22 |
+
index = faiss.IndexFlatL2(corpus_embeddings.shape[1])
|
| 23 |
+
index.add(corpus_embeddings.cpu().numpy())
|
| 24 |
+
|
| 25 |
+
# Initialize Chroma vector store
|
| 26 |
+
client = chromadb.Client()
|
| 27 |
+
collection = client.create_collection(name="faq_data")
|
| 28 |
+
for i, text in enumerate(corpus):
|
| 29 |
+
collection.add(
|
| 30 |
+
documents=[text],
|
| 31 |
+
metadatas=[{"source": f"faq_{i}"}],
|
| 32 |
+
embeddings=[corpus_embeddings[i].cpu().numpy()],
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Retrieval function using FAISS and Chroma
|
| 36 |
def retrieve(query):
|
| 37 |
+
query_embedding = model.encode(query, convert_to_tensor=True).cpu().numpy()
|
| 38 |
+
|
| 39 |
+
# Use FAISS for nearest neighbor search
|
| 40 |
+
faiss_results = index.search(query_embedding, k=1)
|
| 41 |
+
faiss_top_result_idx = faiss_results[1][0][0]
|
| 42 |
+
faiss_top_score = faiss_results[0][0][0]
|
| 43 |
+
|
| 44 |
+
# Use Chroma for semantic search
|
| 45 |
+
chroma_results = collection.query(query_embeddings=[query_embedding], n_results=1)
|
| 46 |
+
chroma_top_result = chroma_results['documents'][0]
|
| 47 |
+
|
| 48 |
+
# Combining results from FAISS and Chroma
|
| 49 |
+
if faiss_top_score > 0.5:
|
| 50 |
+
return answers[corpus[faiss_top_result_idx]]
|
| 51 |
+
else:
|
| 52 |
+
return chroma_top_result or "Sorry, I didn’t understand that. Could you try asking something else?"
|
| 53 |
+
|
| 54 |
+
# Gradio interface to interact with the bot
|
| 55 |
+
iface = gr.Interface(fn=retrieve,
|
| 56 |
+
inputs="text",
|
| 57 |
+
outputs="text",
|
| 58 |
+
live=True,
|
| 59 |
+
title="RAG AI Bot with OCI AI Skills",
|
| 60 |
+
description="Ask me anything related to Hugging Face, Oracle OCI AI, or general knowledge!")
|
| 61 |
|
| 62 |
# Launch the Gradio interface
|
| 63 |
iface.launch()
|