Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,86 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import PyPDF2
|
| 3 |
import io
|
| 4 |
-
import os
|
| 5 |
import time
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
import tempfile
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
text= ""
|
| 13 |
try:
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
pdf_file.seek(0)
|
| 18 |
-
|
| 19 |
else:
|
| 20 |
-
#
|
| 21 |
-
pdf_content= pdf_file
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
for page_num in range(len(pdf_reader.pages)):
|
| 26 |
-
page_text= pdf_reader.pages[page_num].extract_text()
|
| 27 |
-
if page_text:
|
| 28 |
-
|
| 29 |
-
text= text + page_text + "\n"
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
if not text.strip():
|
| 34 |
-
return "
|
| 35 |
-
|
| 36 |
return text
|
| 37 |
except Exception as e:
|
| 38 |
-
return f"Error
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
## create a function to chat with the pdf
|
| 42 |
def chat_with_pdf(api_key, pdf_text, user_question, history):
|
|
|
|
| 43 |
if not api_key.strip():
|
| 44 |
-
return history + [(user_question,"Error:
|
|
|
|
| 45 |
if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
|
| 46 |
-
return history + [(user_question,"Error:
|
|
|
|
| 47 |
if not user_question.strip():
|
| 48 |
-
return history + [(user_question,"Error:
|
| 49 |
-
|
| 50 |
try:
|
| 51 |
-
#
|
| 52 |
-
client= Together(api_key=api_key)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
system_message= f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
|
| 62 |
Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
|
| 63 |
analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
|
| 64 |
PDF CONTENT:
|
| 65 |
{pdf_context}
|
| 66 |
Answer the user's questions only based on the PDF content above. If the answer cannot be found in the PDF, politely state that the information is not available in the provided document."""
|
| 67 |
-
|
| 68 |
-
|
| 69 |
messages = [
|
| 70 |
{"role": "system", "content": system_message},
|
| 71 |
]
|
|
@@ -77,47 +92,57 @@ Answer the user's questions only based on the PDF content above. If the answer c
|
|
| 77 |
|
| 78 |
# Add the current user question
|
| 79 |
messages.append({"role": "user", "content": user_question})
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
temperature= .7,
|
| 88 |
)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
return new_history, new_history
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
-
error_message= f"Error:{str(e)}"
|
| 97 |
return history + [(user_question, error_message)], history
|
| 98 |
|
| 99 |
-
# now process the pdf before the chat
|
| 100 |
def process_pdf(pdf_file, api_key_input):
|
|
|
|
| 101 |
if pdf_file is None:
|
| 102 |
-
return "
|
|
|
|
| 103 |
try:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
if pdf_text.startswith("Error extracting text from PDF"):
|
| 108 |
-
|
|
|
|
| 109 |
if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
|
| 110 |
return f"⚠️ {pdf_text}", "", []
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
# Return a message with the file name and text content
|
| 114 |
status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
|
| 115 |
-
|
| 116 |
-
return
|
|
|
|
| 117 |
except Exception as e:
|
| 118 |
return f"❌ Error processing PDF: {str(e)}", "", []
|
| 119 |
|
| 120 |
-
#
|
| 121 |
def validate_api_key(api_key):
|
| 122 |
"""Simple validation for API key format"""
|
| 123 |
if not api_key or not api_key.strip():
|
|
@@ -128,66 +153,73 @@ def validate_api_key(api_key):
|
|
| 128 |
|
| 129 |
return "✓ API Key format looks valid (not verified with server)"
|
| 130 |
|
| 131 |
-
|
| 132 |
-
with gr.Blocks(title="
|
| 133 |
gr.Markdown("# 📄 ChatPDF with Together AI")
|
| 134 |
gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")
|
| 135 |
-
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=1):
|
|
|
|
| 138 |
api_key_input = gr.Textbox(
|
| 139 |
label="Together API Key",
|
| 140 |
placeholder="Enter your Together API key here...",
|
| 141 |
-
type="password"
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
api_key_status = gr.Textbox(
|
| 144 |
label="API Key Status",
|
| 145 |
interactive=False
|
| 146 |
)
|
| 147 |
-
|
|
|
|
| 148 |
pdf_file = gr.File(
|
| 149 |
label="Upload PDF",
|
| 150 |
file_types=[".pdf"],
|
| 151 |
type="binary" # Ensure we get binary data
|
| 152 |
)
|
| 153 |
-
|
|
|
|
| 154 |
process_button = gr.Button("Process PDF")
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
show_copy_button=True
|
| 171 |
)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
show_copy_button=True
|
| 179 |
-
)
|
| 180 |
-
# Question input
|
| 181 |
-
question = gr.Textbox(
|
| 182 |
-
label="Ask a question about the PDF",
|
| 183 |
-
placeholder="What is the main topic of this document?",
|
| 184 |
-
lines=2
|
| 185 |
)
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
def update_preview(text):
|
| 192 |
"""Update the preview with the first few lines of the PDF text"""
|
| 193 |
if not text or text.startswith("Error") or text.startswith("No text"):
|
|
@@ -198,7 +230,7 @@ with gr.Blocks(title="chatsPDFsupport", theme=gr.themes.Ocean()) as app:
|
|
| 198 |
if len(text) > 500:
|
| 199 |
preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
|
| 200 |
return preview
|
| 201 |
-
|
| 202 |
# API key validation event
|
| 203 |
api_key_input.change(
|
| 204 |
fn=validate_api_key,
|
|
@@ -237,3 +269,4 @@ with gr.Blocks(title="chatsPDFsupport", theme=gr.themes.Ocean()) as app:
|
|
| 237 |
# Launch the app
|
| 238 |
if __name__ == "__main__":
|
| 239 |
app.launch(share=True)
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import PyPDF2
|
| 3 |
import io
|
|
|
|
| 4 |
import time
|
| 5 |
+
import os
|
| 6 |
+
from together import Together
|
| 7 |
+
import textwrap
|
| 8 |
import tempfile
|
| 9 |
|
| 10 |
+
def extract_text_from_pdf(pdf_file):
|
| 11 |
+
"""Extract text from a PDF file"""
|
| 12 |
+
text = ""
|
| 13 |
try:
|
| 14 |
+
# Check if the pdf_file is already in bytes format or needs conversion
|
| 15 |
+
if hasattr(pdf_file, 'read'):
|
| 16 |
+
# If it's a file-like object (from gradio upload)
|
| 17 |
+
pdf_content = pdf_file.read()
|
| 18 |
+
# Reset the file pointer for potential future reads
|
| 19 |
+
if hasattr(pdf_file, 'seek'):
|
| 20 |
pdf_file.seek(0)
|
|
|
|
| 21 |
else:
|
| 22 |
+
# If it's already bytes
|
| 23 |
+
pdf_content = pdf_file
|
| 24 |
+
|
| 25 |
+
# Read the PDF file
|
| 26 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
|
| 27 |
+
|
| 28 |
+
# Extract text from each page
|
| 29 |
for page_num in range(len(pdf_reader.pages)):
|
| 30 |
+
page_text = pdf_reader.pages[page_num].extract_text()
|
| 31 |
+
if page_text: # Check if text extraction worked
|
| 32 |
+
text += page_text + "\n\n"
|
|
|
|
| 33 |
else:
|
| 34 |
+
text += f"[Page {page_num+1} - No extractable text found]\n\n"
|
| 35 |
+
|
| 36 |
if not text.strip():
|
| 37 |
+
return "No text could be extracted from the PDF. The document may be scanned or image-based."
|
| 38 |
+
|
| 39 |
return text
|
| 40 |
except Exception as e:
|
| 41 |
+
return f"Error extracting text from PDF: {str(e)}"
|
| 42 |
|
| 43 |
+
def format_chat_history(history):
|
| 44 |
+
"""Format the chat history for display"""
|
| 45 |
+
formatted_history = []
|
| 46 |
+
for user_msg, bot_msg in history:
|
| 47 |
+
formatted_history.append((user_msg, bot_msg))
|
| 48 |
+
return formatted_history
|
| 49 |
|
|
|
|
| 50 |
def chat_with_pdf(api_key, pdf_text, user_question, history):
|
| 51 |
+
"""Chat with the PDF using Together API"""
|
| 52 |
if not api_key.strip():
|
| 53 |
+
return history + [(user_question, "Error: Please enter your Together API key.")], history
|
| 54 |
+
|
| 55 |
if not pdf_text.strip() or pdf_text.startswith("Error") or pdf_text.startswith("No text"):
|
| 56 |
+
return history + [(user_question, "Error: Please upload a valid PDF file with extractable text first.")], history
|
| 57 |
+
|
| 58 |
if not user_question.strip():
|
| 59 |
+
return history + [(user_question, "Error: Please enter a question.")], history
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
+
# Initialize Together client with the API key
|
| 63 |
+
client = Together(api_key=api_key)
|
| 64 |
+
|
| 65 |
+
# Create the system message with PDF context
|
| 66 |
+
# Truncate the PDF text if it's too long (model context limit handling)
|
| 67 |
+
max_context_length = 10000 #10000
|
| 68 |
+
|
| 69 |
+
if len(pdf_text) > max_context_length:
|
| 70 |
+
# More sophisticated truncation that preserves beginning and end
|
| 71 |
+
half_length = max_context_length // 2
|
| 72 |
+
pdf_context = pdf_text[:half_length] + "\n\n[...Content truncated due to length...]\n\n" + pdf_text[-half_length:]
|
| 73 |
else:
|
| 74 |
+
pdf_context = pdf_text
|
| 75 |
+
|
| 76 |
+
system_message = f"""You are an intelligent assistant designed to read, understand, and extract information from PDF documents.
|
| 77 |
Based on any question or query the user asks—whether it's about content, summaries, data extraction, definitions, insights, or interpretation—you will
|
| 78 |
analyze the following PDF content and provide an accurate, helpful response grounded in the document. Always respond with clear, concise, and context-aware information.
|
| 79 |
PDF CONTENT:
|
| 80 |
{pdf_context}
|
| 81 |
Answer the user's questions only based on the PDF content above. If the answer cannot be found in the PDF, politely state that the information is not available in the provided document."""
|
| 82 |
+
|
| 83 |
+
# Prepare message history for Together API
|
| 84 |
messages = [
|
| 85 |
{"role": "system", "content": system_message},
|
| 86 |
]
|
|
|
|
| 92 |
|
| 93 |
# Add the current user question
|
| 94 |
messages.append({"role": "user", "content": user_question})
|
| 95 |
+
|
| 96 |
+
# Call the Together API
|
| 97 |
+
response = client.chat.completions.create(
|
| 98 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 99 |
+
messages=messages,
|
| 100 |
+
max_tokens=5000, #5000
|
| 101 |
+
temperature=0.7,
|
|
|
|
| 102 |
)
|
| 103 |
+
|
| 104 |
+
# Extract the assistant's response
|
| 105 |
+
assistant_response = response.choices[0].message.content
|
| 106 |
+
|
| 107 |
+
# Update the chat history
|
| 108 |
+
new_history = history + [(user_question, assistant_response)]
|
| 109 |
+
|
| 110 |
return new_history, new_history
|
| 111 |
+
|
| 112 |
except Exception as e:
|
| 113 |
+
error_message = f"Error: {str(e)}"
|
| 114 |
return history + [(user_question, error_message)], history
|
| 115 |
|
|
|
|
| 116 |
def process_pdf(pdf_file, api_key_input):
|
| 117 |
+
"""Process the uploaded PDF file"""
|
| 118 |
if pdf_file is None:
|
| 119 |
+
return "Please upload a PDF file.", "", []
|
| 120 |
+
|
| 121 |
try:
|
| 122 |
+
# Get the file name
|
| 123 |
+
file_name = os.path.basename(pdf_file.name) if hasattr(pdf_file, 'name') else "Uploaded PDF"
|
| 124 |
+
|
| 125 |
+
# Extract text from the PDF
|
| 126 |
+
pdf_text = extract_text_from_pdf(pdf_file)
|
| 127 |
+
|
| 128 |
+
# Check if there was an error in extraction
|
| 129 |
if pdf_text.startswith("Error extracting text from PDF"):
|
| 130 |
+
return f"❌ {pdf_text}", "", []
|
| 131 |
+
|
| 132 |
if not pdf_text.strip() or pdf_text.startswith("No text could be extracted"):
|
| 133 |
return f"⚠️ {pdf_text}", "", []
|
| 134 |
+
|
| 135 |
+
# Count words for information
|
| 136 |
+
word_count = len(pdf_text.split())
|
| 137 |
+
|
| 138 |
# Return a message with the file name and text content
|
| 139 |
status_message = f"✅ Successfully processed PDF: {file_name} ({word_count} words extracted)"
|
| 140 |
+
|
| 141 |
+
# Also return an empty history
|
| 142 |
+
return status_message, pdf_text, []
|
| 143 |
except Exception as e:
|
| 144 |
return f"❌ Error processing PDF: {str(e)}", "", []
|
| 145 |
|
|
|
|
| 146 |
def validate_api_key(api_key):
|
| 147 |
"""Simple validation for API key format"""
|
| 148 |
if not api_key or not api_key.strip():
|
|
|
|
| 153 |
|
| 154 |
return "✓ API Key format looks valid (not verified with server)"
|
| 155 |
|
| 156 |
+
# Create the Gradio interface
|
| 157 |
+
with gr.Blocks(title="ChatPDF with Together AI", theme=gr.themes.Ocean()) as app:
|
| 158 |
gr.Markdown("# 📄 ChatPDF with Together AI")
|
| 159 |
gr.Markdown("Upload a PDF and chat with it using the Llama-3.3-70B model.")
|
| 160 |
+
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column(scale=1):
|
| 163 |
+
# API Key input
|
| 164 |
api_key_input = gr.Textbox(
|
| 165 |
label="Together API Key",
|
| 166 |
placeholder="Enter your Together API key here...",
|
| 167 |
+
type="password"
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# API key validation
|
| 171 |
api_key_status = gr.Textbox(
|
| 172 |
label="API Key Status",
|
| 173 |
interactive=False
|
| 174 |
)
|
| 175 |
+
|
| 176 |
+
# PDF upload
|
| 177 |
pdf_file = gr.File(
|
| 178 |
label="Upload PDF",
|
| 179 |
file_types=[".pdf"],
|
| 180 |
type="binary" # Ensure we get binary data
|
| 181 |
)
|
| 182 |
+
|
| 183 |
+
# Process PDF button
|
| 184 |
process_button = gr.Button("Process PDF")
|
| 185 |
|
| 186 |
+
# Status message
|
| 187 |
+
status_message = gr.Textbox(
|
| 188 |
+
label="Status",
|
| 189 |
+
interactive=False
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
# Hidden field to store the PDF text
|
| 193 |
+
pdf_text = gr.Textbox(visible=False)
|
| 194 |
+
|
| 195 |
+
# Optional: Show PDF preview
|
| 196 |
+
with gr.Accordion("PDF Content Preview", open=False):
|
| 197 |
+
pdf_preview = gr.Textbox(
|
| 198 |
+
label="Extracted Text Preview",
|
| 199 |
+
interactive=False,
|
| 200 |
+
max_lines=10,
|
| 201 |
+
show_copy_button=True
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
with gr.Column(scale=2):
|
| 205 |
+
# Chat interface
|
| 206 |
+
chatbot = gr.Chatbot(
|
| 207 |
+
label="Chat with PDF",
|
| 208 |
+
height=500,
|
| 209 |
show_copy_button=True
|
| 210 |
)
|
| 211 |
+
|
| 212 |
+
# Question input
|
| 213 |
+
question = gr.Textbox(
|
| 214 |
+
label="Ask a question about the PDF",
|
| 215 |
+
placeholder="What is the main topic of this document?",
|
| 216 |
+
lines=2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
)
|
| 218 |
+
|
| 219 |
+
# Submit button
|
| 220 |
+
submit_button = gr.Button("Submit Question")
|
| 221 |
+
|
| 222 |
+
# Event handlers
|
| 223 |
def update_preview(text):
|
| 224 |
"""Update the preview with the first few lines of the PDF text"""
|
| 225 |
if not text or text.startswith("Error") or text.startswith("No text"):
|
|
|
|
| 230 |
if len(text) > 500:
|
| 231 |
preview += "...\n[Text truncated for preview. Full text will be used for chat.]"
|
| 232 |
return preview
|
| 233 |
+
|
| 234 |
# API key validation event
|
| 235 |
api_key_input.change(
|
| 236 |
fn=validate_api_key,
|
|
|
|
| 269 |
# Launch the app
|
| 270 |
if __name__ == "__main__":
|
| 271 |
app.launch(share=True)
|
| 272 |
+
|