willco-afk commited on
Commit
2b509dc
·
verified ·
1 Parent(s): 52bbfc2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import chromadb
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer
5
+ from transformers import pipeline
6
+ import pickle
7
+
8
+ # Load pre-trained model and embeddings
9
+ model = SentenceTransformer("all-MiniLM-L6-v2") # You can upload this model from HF Hub if available
10
+ generator = pipeline("text-generation", model="gpt2")
11
+
12
+ # Initialize ChromaDB client (using the Chroma database uploaded as a file)
13
+ client = chromadb.Client()
14
+ collection = client.create_collection("documents")
15
+
16
+ # Manually load your embeddings and document data from the HF Space files
17
+ with open("embeddings.pkl", "rb") as f:
18
+ embeddings = pickle.load(f)
19
+
20
+ # Example of adding embeddings to FAISS (if using FAISS as the indexer)
21
+ faiss_index = faiss.IndexFlatL2(512) # Adjust dimension if needed
22
+ faiss_index.add(np.array(embeddings))
23
+
24
+ # Example documents loaded manually or fetched via API
25
+ documents = ["What is RAG?", "How does FAISS work?", "Introduction to Chroma."]
26
+
27
+ def generate_answer(query):
28
+ query_embedding = model.encode([query])
29
+ D, I = faiss_index.search(np.array(query_embedding), k=1) # Retrieve the closest document
30
+ retrieved_doc = documents[I[0][0]]
31
+
32
+ prompt = f"Context: {retrieved_doc}\nQuestion: {query}\nAnswer:"
33
+ response = generator(prompt, max_length=50)
34
+ return response[0]['generated_text']
35
+
36
+ # Gradio interface for manual file uploads and query input
37
+ iface = gr.Interface(fn=generate_answer, inputs="text", outputs="text")
38
+ iface.launch()