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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -1,48 +1,58 @@
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 with error handling for encoding issues.
 
 
 
 
11
  """
12
  try:
13
- # Try reading with a lenient encoding (ISO-8859-1) and handle errors gracefully
14
- with open(file_obj.name, 'r', encoding='utf-8', errors='ignore') as file:
15
- context = file.read().strip() # Remove extra spaces or newlines
16
  return context
17
  except Exception as e:
18
  return f"An error occurred: {e}"
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def get_answer(file, question):
21
  context = read_file_content(file)
22
-
23
- if isinstance(context, str) and context.startswith("An error occurred"):
24
- return context # Return the error message from file reading
25
 
26
- if not question.strip():
27
- return "Please provide a valid question."
 
 
 
28
 
29
- try:
30
- print(f"Context:\n{context}") # Debug
31
- print(f"Question: {question}") # Debug
32
-
33
- # Get the answer using the question-answering pipeline
34
- answer = question_answer(question=question, context=context)
35
- return answer["answer"]
36
- except Exception as e:
37
- return f"An error occurred during question answering: {e}"
38
-
39
- # Define the Gradio interface
40
- demo = gr.Interface(
41
- fn=get_answer,
42
- inputs=[gr.File(label="Upload your file"), gr.Textbox(label="Input your question", lines=1)],
43
- outputs=[gr.Textbox(label="Answer text", lines=1)],
44
- title="@GenAILearniverse Project 5: Document Q & A",
45
- description="This application answers questions based on the uploaded context file."
46
- )
47
-
48
- demo.launch()
 
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()