wholewhale commited on
Commit
f421284
1 Parent(s): b13b769
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -13,19 +13,15 @@ pdf_content = ""
13
 
14
  def load_pdf(pdf_doc):
15
  global pdf_content
 
 
16
  try:
17
- if pdf_doc is None:
18
- return "No PDF uploaded."
19
-
20
- # Load PDF content
21
  loader = OnlinePDFLoader(pdf_doc.name)
22
  documents = loader.load()
23
  pdf_content = ' '.join(documents)
24
-
25
- return "PDF Loaded Successfully."
26
-
27
  except Exception as e:
28
- return f"Error processing PDF: {e}"
29
 
30
  def chat_with_pdf(question):
31
  model = ChatAnthropic()
@@ -38,32 +34,26 @@ def chat_with_pdf(question):
38
  response = chain.invoke({})
39
  summarizer = pipeline("summarization")
40
  summary = summarizer(pdf_content, max_length=1000, min_length=30, do_sample=False)[0]['summary_text']
41
- combined_response = f"Summary: {summary}\n\nChat Response: {response.content}"
42
- return combined_response
43
 
44
  def gradio_interface(pdf_doc, question):
45
  if not pdf_content:
46
  return load_pdf(pdf_doc)
47
  else:
48
- summarizer = pipeline("summarization")
49
- summary = summarizer(pdf_content, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
50
- response = chat_with_pdf(question)
51
- return {
52
- "Summary": summary,
53
- "Chat Response": response
54
- }
55
 
56
  gr.Interface(
57
  fn=gradio_interface,
58
  inputs=[
59
- gr.File(label="Load a pdf", file_types=['.pdf'], type="file"),
60
- gr.Textbox(label="Ask a question about the PDF")
61
  ],
62
  outputs=[
63
- gr.outputs.Textbox(label="Summary"),
64
- gr.outputs.Textbox(label="Chat Response")
65
  ],
66
  live=True,
67
  title="Chat with PDF content using Anthropic",
68
- description="Upload a .PDF and interactively chat about its content."
 
69
  ).launch()
 
13
 
14
  def load_pdf(pdf_doc):
15
  global pdf_content
16
+ if pdf_doc is None:
17
+ return "No PDF uploaded.", ""
18
  try:
 
 
 
 
19
  loader = OnlinePDFLoader(pdf_doc.name)
20
  documents = loader.load()
21
  pdf_content = ' '.join(documents)
22
+ return "PDF Loaded Successfully.", pdf_content
 
 
23
  except Exception as e:
24
+ return f"Error processing PDF: {e}", ""
25
 
26
  def chat_with_pdf(question):
27
  model = ChatAnthropic()
 
34
  response = chain.invoke({})
35
  summarizer = pipeline("summarization")
36
  summary = summarizer(pdf_content, max_length=1000, min_length=30, do_sample=False)[0]['summary_text']
37
+ return summary, response.content
 
38
 
39
  def gradio_interface(pdf_doc, question):
40
  if not pdf_content:
41
  return load_pdf(pdf_doc)
42
  else:
43
+ return chat_with_pdf(question)
 
 
 
 
 
 
44
 
45
  gr.Interface(
46
  fn=gradio_interface,
47
  inputs=[
48
+ gr.components.File(label="Load a pdf", file_types=['.pdf'], type="file"),
49
+ gr.components.Textbox(label="Ask a question about the PDF")
50
  ],
51
  outputs=[
52
+ gr.components.Textbox(label="Summary"),
53
+ gr.components.Textbox(label="Chat Response")
54
  ],
55
  live=True,
56
  title="Chat with PDF content using Anthropic",
57
+ description="Upload a .PDF and interactively chat about its content.",
58
+ api_name='chat_with_pdf_3' # Changing api_name to avoid conflicts
59
  ).launch()