Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from sentence_transformers import SentenceTransformer, util
|
4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import PyPDF2
|
6 |
import os
|
7 |
import time
|
8 |
import logging
|
|
|
9 |
|
10 |
# Set up logging
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
logger = logging.getLogger(__name__)
|
|
|
13 |
|
14 |
-
# Load
|
15 |
-
|
16 |
-
retriever_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
17 |
-
gen_tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1")
|
18 |
-
gen_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1", torch_dtype=torch.float16)
|
19 |
-
logger.info("Models loaded successfully")
|
20 |
-
except Exception as e:
|
21 |
-
logger.error(f"Error loading models: {str(e)}")
|
22 |
-
raise
|
23 |
|
24 |
# Cache for document embeddings
|
25 |
embedding_cache = {}
|
@@ -57,7 +51,7 @@ def chunk_text(text, chunk_size=500):
|
|
57 |
return chunks
|
58 |
|
59 |
def get_document_embeddings(documents):
|
60 |
-
"""Compute embeddings for documents, using cache if available
|
61 |
embeddings = []
|
62 |
for doc in documents:
|
63 |
if doc in embedding_cache:
|
@@ -68,25 +62,23 @@ def get_document_embeddings(documents):
|
|
68 |
embeddings.append(emb)
|
69 |
return torch.stack(embeddings)
|
70 |
|
71 |
-
def
|
72 |
-
"""
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
logger.error(f"Error in generate_response: {str(e)}")
|
82 |
-
return f"Generation error: {str(e)}"
|
83 |
|
84 |
def rag_pipeline(question, pdf_files):
|
85 |
-
"""RAG pipeline with
|
86 |
start_time = time.time()
|
87 |
documents = []
|
88 |
|
89 |
-
# Process PDFs
|
90 |
if pdf_files:
|
91 |
for pdf in pdf_files:
|
92 |
pages = extract_text_from_pdf(pdf)
|
@@ -94,7 +86,6 @@ def rag_pipeline(question, pdf_files):
|
|
94 |
chunks = chunk_text(page)
|
95 |
documents.extend(chunks)
|
96 |
else:
|
97 |
-
# Default documents relevant to AI and Data Science
|
98 |
documents = [
|
99 |
"Artificial Intelligence (AI) is the simulation of human intelligence in machines.",
|
100 |
"Data Science involves extracting insights from structured and unstructured data using statistical methods.",
|
@@ -105,55 +96,57 @@ def rag_pipeline(question, pdf_files):
|
|
105 |
if not documents:
|
106 |
return "No valid text could be extracted from the PDFs."
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
# Embed the query
|
112 |
-
query_embedding = retriever_model.encode(question, convert_to_tensor=True)
|
113 |
-
|
114 |
-
# Retrieve top 3 chunks using cosine similarity
|
115 |
-
cos_scores = util.pytorch_cos_sim(query_embedding, doc_embeddings)[0]
|
116 |
-
top_results = torch.topk(cos_scores, k=min(3, len(documents)))
|
117 |
-
retrieved_context = ""
|
118 |
-
for score, idx in zip(top_results.values, top_results.indices):
|
119 |
-
retrieved_context += f"- {documents[idx]} (score: {score:.2f})\n"
|
120 |
-
|
121 |
logger.info(f"Retrieved context:\n{retrieved_context}")
|
122 |
|
123 |
-
#
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
)
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
141 |
)
|
142 |
-
refined_answer = generate_response(refine_prompt)
|
143 |
-
if "Generation error" in refined_answer:
|
144 |
-
return refined_answer
|
145 |
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
logger.info(f"Processing time: {time.time() - start_time:.2f} seconds")
|
149 |
-
return
|
150 |
|
151 |
# Gradio UI
|
152 |
with gr.Blocks() as demo:
|
153 |
-
gr.Markdown("# RAG Pipeline with microsoft/phi-1 and
|
154 |
gr.Markdown(
|
155 |
"Upload PDFs (or use default AI/Data Science docs), ask a question, "
|
156 |
-
"and get refined answers using Phi-1
|
157 |
)
|
158 |
with gr.Row():
|
159 |
with gr.Column():
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from sentence_transformers import SentenceTransformer, util
|
|
|
4 |
import PyPDF2
|
5 |
import os
|
6 |
import time
|
7 |
import logging
|
8 |
+
from yacana import Agent, Task, LoggerManager
|
9 |
|
10 |
# Set up logging
|
11 |
logging.basicConfig(level=logging.INFO)
|
12 |
logger = logging.getLogger(__name__)
|
13 |
+
yacana_logger = LoggerManager()
|
14 |
|
15 |
+
# Load retriever model
|
16 |
+
retriever_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Cache for document embeddings
|
19 |
embedding_cache = {}
|
|
|
51 |
return chunks
|
52 |
|
53 |
def get_document_embeddings(documents):
|
54 |
+
"""Compute embeddings for documents, using cache if available."""
|
55 |
embeddings = []
|
56 |
for doc in documents:
|
57 |
if doc in embedding_cache:
|
|
|
62 |
embeddings.append(emb)
|
63 |
return torch.stack(embeddings)
|
64 |
|
65 |
+
def retrieve_context(question, documents):
|
66 |
+
"""Retrieve top 3 relevant chunks."""
|
67 |
+
doc_embeddings = get_document_embeddings(documents)
|
68 |
+
query_embedding = retriever_model.encode(question, convert_to_tensor=True)
|
69 |
+
cos_scores = util.pytorch_cos_sim(query_embedding, doc_embeddings)[0]
|
70 |
+
top_results = torch.topk(cos_scores, k=min(3, len(documents)))
|
71 |
+
retrieved_context = ""
|
72 |
+
for score, idx in zip(top_results.values, top_results.indices):
|
73 |
+
retrieved_context += f"- {documents[idx]} (score: {score:.2f})\n"
|
74 |
+
return retrieved_context
|
|
|
|
|
75 |
|
76 |
def rag_pipeline(question, pdf_files):
|
77 |
+
"""RAG pipeline with Yacana and Phi-1."""
|
78 |
start_time = time.time()
|
79 |
documents = []
|
80 |
|
81 |
+
# Process PDFs or use default documents
|
82 |
if pdf_files:
|
83 |
for pdf in pdf_files:
|
84 |
pages = extract_text_from_pdf(pdf)
|
|
|
86 |
chunks = chunk_text(page)
|
87 |
documents.extend(chunks)
|
88 |
else:
|
|
|
89 |
documents = [
|
90 |
"Artificial Intelligence (AI) is the simulation of human intelligence in machines.",
|
91 |
"Data Science involves extracting insights from structured and unstructured data using statistical methods.",
|
|
|
96 |
if not documents:
|
97 |
return "No valid text could be extracted from the PDFs."
|
98 |
|
99 |
+
# Retrieve context
|
100 |
+
retrieved_context = retrieve_context(question, documents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
logger.info(f"Retrieved context:\n{retrieved_context}")
|
102 |
|
103 |
+
# Define Yacana agents and tasks
|
104 |
+
agent = Agent("Phi1Agent", "phi", logger=yacana_logger) # Assumes phi-1 via Ollama
|
105 |
+
|
106 |
+
# Task 1: Initial Answer
|
107 |
+
initial_task = Task(
|
108 |
+
name="GenerateInitialAnswer",
|
109 |
+
instruction=(
|
110 |
+
f"Using the following context, provide a concise answer to the question:\n\n"
|
111 |
+
f"Context:\n{retrieved_context}\n\n"
|
112 |
+
f"Question: {question}\n\n"
|
113 |
+
f"Answer:"
|
114 |
+
),
|
115 |
+
agent=agent
|
116 |
)
|
117 |
+
|
118 |
+
# Task 2: Refine Answer
|
119 |
+
initial_result = initial_task.run()
|
120 |
+
refine_task = Task(
|
121 |
+
name="RefineAnswer",
|
122 |
+
instruction=(
|
123 |
+
f"Given the context and initial answer, refine and improve the response:\n\n"
|
124 |
+
f"Context:\n{retrieved_context}\n\n"
|
125 |
+
f"Question: {question}\n\n"
|
126 |
+
f"Initial Answer: {initial_result}\n\n"
|
127 |
+
f"Refined Answer:"
|
128 |
+
),
|
129 |
+
agent=agent
|
130 |
)
|
|
|
|
|
|
|
131 |
|
132 |
+
# Execute tasks
|
133 |
+
try:
|
134 |
+
refined_result = refine_task.run()
|
135 |
+
logger.info(f"Initial answer: {initial_result}")
|
136 |
+
logger.info(f"Refined answer: {refined_result}")
|
137 |
+
except Exception as e:
|
138 |
+
logger.error(f"Error in Yacana tasks: {str(e)}")
|
139 |
+
return f"Task execution error: {str(e)}"
|
140 |
+
|
141 |
logger.info(f"Processing time: {time.time() - start_time:.2f} seconds")
|
142 |
+
return refined_result if refined_result else "Unable to generate a meaningful response."
|
143 |
|
144 |
# Gradio UI
|
145 |
with gr.Blocks() as demo:
|
146 |
+
gr.Markdown("# RAG Pipeline with microsoft/phi-1 and Yacana")
|
147 |
gr.Markdown(
|
148 |
"Upload PDFs (or use default AI/Data Science docs), ask a question, "
|
149 |
+
"and get refined answers using Phi-1 via Yacana on 2 vCPUs and 16GB RAM."
|
150 |
)
|
151 |
with gr.Row():
|
152 |
with gr.Column():
|