Spaces:
Sleeping
Sleeping
File size: 6,975 Bytes
6e5d3d5 ec73a76 6e5d3d5 ec73a76 6e5d3d5 7723293 6e5d3d5 7723293 6e5d3d5 ec73a76 7384705 ec73a76 7384705 ec73a76 6e5d3d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import gradio as gr
import fitz # PyMuPDF
import os
import tempfile
import requests
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from datetime import datetime
# === CONFIG CHECK ===
if not os.getenv("GROQ_API_KEY"):
print("WARNING: GROQ_API_KEY environment variable not set. API calls will fail.")
# === Globals ===
vectorizer = TfidfVectorizer(stop_words='english')
# === UTILITY FUNCTIONS ===
""" def call_groq_api(prompt):
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
return "Error: GROQ_API_KEY environment variable not set."
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
data = {"model": "llama-3.1-70b-versatile", "messages": [{"role": "user", "content": prompt}]}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=data, headers=headers)
if response.status_code != 200:
return f"API Error {response.status_code}: {response.text}"
return response.json()["choices"][0]["message"]["content"]
except requests.exceptions.RequestException as e:
return f"API Error: {str(e)}"
except (KeyError, IndexError) as e:
return f"Error parsing API response: {str(e)}"
"""
def call_groq_api(prompt):
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
return "Error: GROQ_API_KEY environment variable not set."
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"model": "llama-3.3-70b-versatile",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7
}
try:
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=data, headers=headers)
if response.status_code != 200:
return f"API Error {response.status_code}: {response.text}"
result = response.json()
return result["choices"][0]["message"]["content"]
except requests.exceptions.RequestException as e:
return f"Network Error: {e}"
except Exception as e:
return f"Unexpected Error: {e}"
def extract_text_from_pdfs(pdf_files):
chunks, pages, file_names = [], [], []
for file in pdf_files:
try:
doc = fitz.open(file.name)
for page_num, page in enumerate(doc, start=1):
text = page.get_text().strip()
if text:
chunks.append(text)
pages.append(page_num)
file_names.append(os.path.basename(file.name))
except Exception as e:
print(f"Error processing {file.name}: {e}")
return chunks, pages, file_names
def retrieve_context(query, chunks, pages, file_names, top_k=3):
all_texts = chunks + [query]
tfidf_matrix = vectorizer.fit_transform(all_texts)
query_vec = tfidf_matrix[-1]
similarities = cosine_similarity(query_vec, tfidf_matrix[:-1]).flatten()
if max(similarities) < 0.2:
return "Ask a relevant question.", [], []
top_indices = similarities.argsort()[-top_k:][::-1]
selected_chunks = [chunks[i] for i in top_indices]
references = [f"{file_names[i]} (p.{pages[i]})" for i in top_indices]
return "\n".join(selected_chunks), selected_chunks, references
def download_chat(chat_history):
if not chat_history:
return None
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"chat_{timestamp}.txt"
path = os.path.join(tempfile.gettempdir(), filename)
with open(path, "w", encoding="utf-8") as f:
for q, a in chat_history:
f.write(f"Q: {q}\nA: {a}\n\n")
return path
# === Main Q&A Logic ===
def answer_question(text_input, pdf_files, chat_history):
if chat_history is None:
chat_history = []
if not text_input:
return "β Please type a question.", chat_history, chat_history
if not pdf_files:
return "β Please upload PDF files first.", chat_history, chat_history
chunks, pages, file_names = extract_text_from_pdfs(pdf_files)
if not chunks:
return "β Could not extract text from PDFs.", chat_history, chat_history
context, matched_chunks, references = retrieve_context(text_input, chunks, pages, file_names)
if context == "Ask a relevant question.":
response = "β οΈ Ask a relevant question based on the PDFs."
chat_history.append([text_input, response])
return response, chat_history, chat_history
prompt = f"Answer the question using this context:\n\n{context}\n\nQuestion: {text_input}\n\nAnswer:"
answer = call_groq_api(prompt)
full_answer = f"{answer}\n\nπ Sources: {', '.join(references)}"
chat_history.append([text_input, full_answer])
return full_answer, chat_history, chat_history
# === Custom CSS ===
custom_css = """
.gradio-container {
max-width: 900px !important;
margin: auto;
font-family: 'Segoe UI', sans-serif;
}
body {
background-color: var(--background-primary);
color: var(--body-text-color);
}
textarea, input, button {
font-family: 'Segoe UI', sans-serif !important;
}
"""
# === Launch UI ===
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
gr.Markdown("""
# π§ **SmartPDF Q&A Bot**
_Ask questions from your PDFs. Get answers with page references. Download chat history._
""", elem_id="title")
chat_state = gr.State([])
with gr.Tabs():
with gr.Tab("π Upload PDFs"):
gr.Markdown("### Step 1: Upload one or more PDF documents.")
pdf_input = gr.File(label="π Upload PDF Files", file_types=[".pdf"], file_count="multiple")
with gr.Tab("π¬ Ask Questions"):
gr.Markdown("### Step 2: Ask a question about the uploaded documents.")
with gr.Row():
text_input = gr.Textbox(label="β Type your question here", placeholder="e.g. What is the main idea of the first document?", lines=2)
ask_btn = gr.Button("π Ask")
answer_output = gr.Textbox(label="π§ Answer", lines=6)
chatbox = gr.Dataframe(headers=["User", "Bot"], label="π¬ Chat History", interactive=False)
with gr.Tab("π₯ Export Chat History"):
gr.Markdown("### Step 3: Download your chat session.")
download_btn = gr.Button("β¬οΈ Download Chat History")
download_file = gr.File(label="π Your Chat File", visible=False)
# === Button Event Binding ===
ask_btn.click(
answer_question,
inputs=[text_input, pdf_input, chat_state],
outputs=[answer_output, chatbox, chat_state]
)
download_btn.click(
download_chat,
inputs=[chat_state],
outputs=download_file
).then(lambda: gr.update(visible=True), None, [download_file])
if __name__ == "__main__":
demo.launch(share=True)
|