Muhammad Anas Akhtar commited on
Commit
97486f8
·
verified ·
1 Parent(s): 87d7845

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -34
app.py CHANGED
@@ -1,58 +1,44 @@
1
  import torch
2
  import gradio as gr
3
-
4
- # Use a pipeline as a high-level helper
5
  from transformers import pipeline
6
 
7
  model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
8
  "/cbf50ba81465d4d8676b8bab348e31835147541b")
9
 
10
- question_answer = pipeline("question-answering",
11
- model="deepset/roberta-base-squad2")
12
-
13
- # question_answer = pipeline("question-answering",
14
- # model=model_path)
15
 
16
- def read_file_content(file_obj):
17
  """
18
- Reads the content of a file object and returns it.
19
  Parameters:
20
  file_obj (file object): The file object to read from.
21
  Returns:
22
- str: The content of the file.
23
  """
24
  try:
25
- with open(file_obj.name, 'r', encoding='utf-8') as file:
26
- context = file.read()
27
- return context
 
 
28
  except Exception as e:
29
  return f"An error occurred: {e}"
30
 
31
- # Example usage:
32
- # with open('example.txt', 'r') as file:
33
- # content = read_file_content(file)
34
- # print(content)
35
-
36
-
37
- # context =("Mark Elliot Zuckerberg (/ˈzʌkərbɜːrɡ/; born May 14, 1984) is an American businessman. He co-founded the social media service Facebook, along with his Harvard roommates in 2004, and its parent company Meta Platforms (formerly Facebook, Inc.), of which he is chairman, chief executive officer and controlling shareholder.Zuckerberg briefly attended Harvard University, where he launched Facebook "
38
- # "in February 2004 with his roommates Eduardo Saverin, Andrew McCollum, "
39
- # "Dustin Moskovitz and Chris Hughes. Zuckerberg took the company public in May 2012 with "
40
- # "majority shares. In 2008, at age 23, he became the world's youngest self-made billionaire. "
41
- # "He has since used his funds to organize multiple donations, including the establishment "
42
- # "of the Chan Zuckerberg Initiative.")
43
- # question ="what is Mark's DOB?"
44
-
45
-
46
-
47
  def get_answer(file, question):
48
- context = read_file_content(file)
 
 
 
 
 
49
  answer = question_answer(question=question, context=context)
50
  return answer["answer"]
51
 
52
  demo = gr.Interface(fn=get_answer,
53
- inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question",lines=1)],
54
- outputs=[gr.Textbox(label="Answer text",lines=1)],
55
  title="@GenAILearniverse Project 5: Document Q & A",
56
- description="THIS APPLICATION WILL BE USED TO ANSER QUESTIONS BASED ON CONTEXT PROVIDED.")
57
 
58
- demo.launch()
 
1
  import torch
2
  import gradio as gr
3
+ import pdfplumber
 
4
  from transformers import pipeline
5
 
6
  model_path = ("../Models/models--deepset--roberta-base-squad2/snapshots"
7
  "/cbf50ba81465d4d8676b8bab348e31835147541b")
8
 
9
+ question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
 
 
 
 
10
 
11
+ def read_pdf_content(file_obj):
12
  """
13
+ Reads the content of a PDF file object and returns the extracted text.
14
  Parameters:
15
  file_obj (file object): The file object to read from.
16
  Returns:
17
+ str: The extracted text from the PDF.
18
  """
19
  try:
20
+ with pdfplumber.open(file_obj) as pdf:
21
+ text = ""
22
+ for page in pdf.pages:
23
+ text += page.extract_text()
24
+ return text
25
  except Exception as e:
26
  return f"An error occurred: {e}"
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def get_answer(file, question):
29
+ # Extract text from the uploaded PDF
30
+ context = read_pdf_content(file)
31
+ if context.startswith("An error occurred"):
32
+ return context
33
+
34
+ # Get the answer from the model
35
  answer = question_answer(question=question, context=context)
36
  return answer["answer"]
37
 
38
  demo = gr.Interface(fn=get_answer,
39
+ inputs=[gr.File(label="Upload your PDF file"), gr.Textbox(label="Input your question", lines=1)],
40
+ outputs=[gr.Textbox(label="Answer text", lines=1)],
41
  title="@GenAILearniverse Project 5: Document Q & A",
42
+ description="This application will be used to answer questions based on context provided from the uploaded PDF.")
43
 
44
+ demo.launch()