shukdevdatta123 commited on
Commit
a723167
·
verified ·
1 Parent(s): 27faa49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -82,17 +82,26 @@ def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
82
  if pdf_file is None or not text_query:
83
  return "Please upload a PDF and provide a query."
84
 
85
- # Extract text from all pages
86
- doc = fitz.open(pdf_file)
87
- text = "\n".join([page.get_text("text") for page in doc]) # Extract text from all pages
88
-
89
- messages = [
90
- {"role": "user", "content": [
91
- {"type": "text", "text": text}, # Fixed format
92
- {"type": "text", "text": text_query}
93
- ]},
94
- ]
95
- return query_openai(messages, temperature, top_p, max_output_tokens)
 
 
 
 
 
 
 
 
 
96
 
97
  # Function to transcribe audio to text using OpenAI Whisper API
98
  def transcribe_audio(audio_binary, openai_api_key):
@@ -201,4 +210,4 @@ with gr.Blocks() as demo:
201
 
202
  # Launch Gradio App
203
  if __name__ == "__main__":
204
- demo.launch()
 
82
  if pdf_file is None or not text_query:
83
  return "Please upload a PDF and provide a query."
84
 
85
+ try:
86
+ # Extract text from all pages of the PDF
87
+ doc = fitz.open(pdf_file.name)
88
+ text = "\n".join([page.get_text("text") for page in doc]) # Extract text from all pages
89
+
90
+ # If no text found, return an error
91
+ if not text.strip():
92
+ return "No text found in the PDF."
93
+
94
+ # Create the query message with the extracted text and the user's query
95
+ messages = [
96
+ {"role": "user", "content": [
97
+ {"type": "text", "text": text}, # The extracted text from the PDF
98
+ {"type": "text", "text": text_query}
99
+ ]},
100
+ ]
101
+ return query_openai(messages, temperature, top_p, max_output_tokens)
102
+
103
+ except Exception as e:
104
+ return f"Error processing the PDF: {str(e)}"
105
 
106
  # Function to transcribe audio to text using OpenAI Whisper API
107
  def transcribe_audio(audio_binary, openai_api_key):
 
210
 
211
  # Launch Gradio App
212
  if __name__ == "__main__":
213
+ demo.launch()