Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,161 +1,92 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
from sentence_transformers import SentenceTransformer
|
7 |
-
|
8 |
-
# ---------------------------
|
9 |
-
# Load Models (cached on first run)
|
10 |
-
# ---------------------------
|
11 |
-
def load_models():
|
12 |
-
hf_token = os.getenv("HF_TOKEN") # Set this secret in your HF Space settings
|
13 |
-
embed_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') # For embeddings
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-4b-it", use_auth_token=hf_token)
|
15 |
-
model = AutoModelForCausalLM.from_pretrained(
|
16 |
-
"google/gemma-3-4b-it",
|
17 |
-
device_map="auto",
|
18 |
-
low_cpu_mem_usage=True,
|
19 |
-
use_auth_token=hf_token
|
20 |
-
)
|
21 |
-
return embed_model, tokenizer, model
|
22 |
-
|
23 |
-
embed_model, tokenizer, model = load_models()
|
24 |
|
25 |
-
#
|
26 |
-
#
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
-
"doc_chunks": []
|
31 |
-
}
|
32 |
|
33 |
-
|
34 |
-
# Document Processing Function
|
35 |
-
# ---------------------------
|
36 |
-
def process_document(file, chunk_size, chunk_overlap):
|
37 |
"""
|
38 |
-
|
39 |
-
computes embeddings, and builds a FAISS index.
|
40 |
"""
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return "Error: PyPDF2 is required for PDF extraction."
|
53 |
-
# Save file to temporary path
|
54 |
-
temp_path = os.path.join("temp", file_name)
|
55 |
-
os.makedirs("temp", exist_ok=True)
|
56 |
-
with open(temp_path, "wb") as f:
|
57 |
-
f.write(file_bytes)
|
58 |
-
reader = PdfReader(temp_path)
|
59 |
-
for page in reader.pages:
|
60 |
-
text += page.extract_text() or ""
|
61 |
-
else:
|
62 |
-
# Assume it's a text file
|
63 |
-
text = file_bytes.decode("utf-8", errors="ignore")
|
64 |
-
|
65 |
-
if text.strip() == "":
|
66 |
-
return "No text found in the document."
|
67 |
-
|
68 |
-
# Split text into overlapping chunks
|
69 |
-
chunks = []
|
70 |
-
for start in range(0, len(text), chunk_size - chunk_overlap):
|
71 |
-
chunk_text = text[start: start + chunk_size]
|
72 |
-
chunks.append(chunk_text)
|
73 |
-
|
74 |
-
# Compute embeddings for each chunk using the embedding model.
|
75 |
-
embeddings = embed_model.encode(chunks, normalize_embeddings=True).astype('float32')
|
76 |
-
dim = embeddings.shape[1]
|
77 |
-
|
78 |
-
# Build FAISS index using cosine similarity (normalized vectors -> inner product)
|
79 |
-
index = faiss.IndexFlatIP(dim)
|
80 |
-
index.add(embeddings)
|
81 |
-
|
82 |
-
# Update global state
|
83 |
-
state["faiss_index"] = index
|
84 |
-
state["doc_chunks"] = chunks
|
85 |
-
|
86 |
-
# Return a preview (first 500 characters of the first chunk) and status.
|
87 |
-
preview = chunks[0][:500] if chunks else "No content"
|
88 |
-
return f"Indexed {len(chunks)} chunks.\n\n**Document Preview:**\n{preview}"
|
89 |
|
90 |
-
|
91 |
-
# Question Answering Function
|
92 |
-
# ---------------------------
|
93 |
-
def answer_question(query, top_k):
|
94 |
"""
|
95 |
-
|
96 |
-
|
|
|
97 |
"""
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
|
108 |
-
retrieved_text = ""
|
109 |
-
for idx in I[0]:
|
110 |
-
retrieved_text += chunks[idx] + "\n"
|
111 |
|
112 |
-
|
113 |
-
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
120 |
|
121 |
-
|
122 |
-
#
|
123 |
-
# ---------------------------
|
124 |
-
with gr.Blocks(title="RAG System with Gemma‑3‑4B‑it") as demo:
|
125 |
gr.Markdown(
|
126 |
-
""
|
127 |
-
|
128 |
-
|
129 |
-
build a vector index using FAISS, and then allow you to ask questions based on the document.
|
130 |
-
"""
|
131 |
)
|
132 |
|
133 |
-
with gr.
|
134 |
-
with gr.
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
top_k_input = gr.Number(label="Number of Chunks to Retrieve", value=3, precision=0)
|
145 |
-
answer_btn = gr.Button("Get Answer")
|
146 |
-
answer_output = gr.Markdown(label="Answer")
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
fn=process_document,
|
151 |
-
inputs=[file_input, chunk_size_input, chunk_overlap_input],
|
152 |
-
outputs=process_output
|
153 |
-
)
|
154 |
-
answer_btn.click(
|
155 |
-
fn=answer_question,
|
156 |
-
inputs=[query_input, top_k_input],
|
157 |
-
outputs=answer_output
|
158 |
-
)
|
159 |
-
|
160 |
-
if __name__ == "__main__":
|
161 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
import PyPDF2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Set your Hugging Face API token.
|
7 |
+
# Option 1: Set it as an environment variable named "HF_API_TOKEN".
|
8 |
+
# Option 2: Replace "YOUR_HUGGINGFACE_API_TOKEN" with your token directly.
|
9 |
+
API_TOKEN = os.environ.get("HF_TOKEN", "YOUR_HUGGINGFACE_API_TOKEN")
|
10 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
|
|
|
11 |
|
12 |
+
def extract_pdf_text(pdf_file):
|
|
|
|
|
|
|
13 |
"""
|
14 |
+
Extracts text from a PDF file using PyPDF2.
|
|
|
15 |
"""
|
16 |
+
pdf_text = ""
|
17 |
+
try:
|
18 |
+
with open(pdf_file, "rb") as f:
|
19 |
+
reader = PyPDF2.PdfReader(f)
|
20 |
+
for page in reader.pages:
|
21 |
+
text = page.extract_text()
|
22 |
+
if text:
|
23 |
+
pdf_text += text + "\n"
|
24 |
+
except Exception as e:
|
25 |
+
print("Error reading PDF:", e)
|
26 |
+
return pdf_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def generate_response(query, pdf_file=None):
|
|
|
|
|
|
|
29 |
"""
|
30 |
+
If a PDF file is uploaded, extract its text and combine a limited part of it
|
31 |
+
with the user query to form a prompt. Then send the prompt to the Hugging Face
|
32 |
+
Inference API using the RAG model.
|
33 |
"""
|
34 |
+
pdf_text = ""
|
35 |
+
if pdf_file is not None:
|
36 |
+
pdf_text = extract_pdf_text(pdf_file)
|
37 |
+
|
38 |
+
# If PDF text is available, append its (truncated) content as context
|
39 |
+
if pdf_text:
|
40 |
+
# Limit the context to avoid token overflow; adjust as needed.
|
41 |
+
context = pdf_text[:2000]
|
42 |
+
full_input = "Context: " + context + "\n\nQuestion: " + query
|
43 |
+
else:
|
44 |
+
full_input = query
|
45 |
+
|
46 |
+
# Define the model and endpoint for the RAG model.
|
47 |
+
model_id = "facebook/rag-token-nq"
|
48 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
49 |
|
50 |
+
payload = {"inputs": full_input}
|
|
|
|
|
|
|
51 |
|
52 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
53 |
+
if response.status_code != 200:
|
54 |
+
return "Error: " + response.text
|
55 |
|
56 |
+
result = response.json()
|
57 |
+
# Extract the generated text if available.
|
58 |
+
if isinstance(result, list) and result and "generated_text" in result[0]:
|
59 |
+
return result[0]["generated_text"]
|
60 |
+
else:
|
61 |
+
return str(result)
|
62 |
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
gr.Markdown("# Retrieval Augmented Generation (RAG) Chatbot with PDF Input")
|
|
|
|
|
65 |
gr.Markdown(
|
66 |
+
"Powered by the Hugging Face Inference API. "
|
67 |
+
"Optionally upload a PDF file and ask a question related to its content. "
|
68 |
+
"If no PDF is uploaded, the model will answer based solely on the query."
|
|
|
|
|
69 |
)
|
70 |
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column():
|
73 |
+
pdf_input = gr.File(label="Upload PDF (optional)", file_types=[".pdf"])
|
74 |
+
query_input = gr.Textbox(label="Your Question", placeholder="Type your question here...", lines=3)
|
75 |
+
submit_button = gr.Button("Submit")
|
76 |
+
gr.Examples(
|
77 |
+
examples=[
|
78 |
+
["What is the main argument in the document?"],
|
79 |
+
["Summarize the content of the PDF."],
|
80 |
+
["What conclusions can be drawn from the report?"],
|
81 |
+
],
|
82 |
+
inputs=query_input,
|
83 |
+
label="Try one of these examples:"
|
84 |
+
)
|
85 |
+
with gr.Column():
|
86 |
+
response_output = gr.Textbox(label="Response", placeholder="The answer will appear here...", lines=10)
|
87 |
|
88 |
+
# Link the button click to the generate_response function.
|
89 |
+
submit_button.click(fn=generate_response, inputs=[query_input, pdf_input], outputs=response_output)
|
|
|
|
|
|
|
90 |
|
91 |
+
# Launch the app locally
|
92 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|