Muhammad Anas Akhtar commited on
Commit
d74cbad
·
verified ·
1 Parent(s): 059ad72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -40
app.py CHANGED
@@ -1,58 +1,43 @@
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
  from transformers import pipeline
4
 
5
+ # Use the Hugging Face model pipeline
6
+ question_answer = pipeline("question-answering", model="deepset/roberta-base-squad2")
 
 
 
 
 
 
7
 
8
  def read_file_content(file_obj):
9
  """
10
+ Reads the content of a file object, cleans it, and returns it.
 
 
 
 
11
  """
12
  try:
13
  with open(file_obj.name, 'r', encoding='utf-8') as file:
14
+ context = file.read().strip()
15
  return context
16
  except Exception as e:
17
  return f"An error occurred: {e}"
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def get_answer(file, question):
20
  context = read_file_content(file)
21
+ if isinstance(context, str) and context.startswith("An error occurred"):
22
+ return context # Return file reading error
23
+
24
+ if not question.strip():
25
+ return "Please provide a valid question."
26
+
27
+ try:
28
+ print(f"Context:\n{context}") # Debug
29
+ print(f"Question: {question}") # Debug
30
+ answer = question_answer(question=question, context=context)
31
+ return answer["answer"]
32
+ except Exception as e:
33
+ return f"An error occurred during question answering: {e}"
34
 
35
+ demo = gr.Interface(
36
+ fn=get_answer,
37
+ inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question", lines=1)],
38
+ outputs=[gr.Textbox(label="Answer text", lines=1)],
39
+ title="@GenAILearniverse Project 5: Document Q & A",
40
+ description="This application answers questions based on the uploaded context file."
41
+ )
42
 
43
+ demo.launch()