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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -48
app.py CHANGED
@@ -1,67 +1,58 @@
1
  import torch
2
  import gradio as gr
3
- from transformers import pipeline
4
 
5
  # Use a pipeline as a high-level helper
 
 
 
 
 
6
  question_answer = pipeline("question-answering",
7
- model="deepset/roberta-base-squad2")
 
 
 
8
 
9
  def read_file_content(file_obj):
10
  """
11
  Reads the content of a file object and returns it.
12
- Attempts multiple encodings if the default fails.
13
-
14
  Parameters:
15
  file_obj (file object): The file object to read from.
16
  Returns:
17
  str: The content of the file.
18
  """
19
- encodings = ['utf-8', 'latin-1', 'cp1252', 'ascii']
20
-
21
- for encoding in encodings:
22
- try:
23
- with open(file_obj.name, 'r', encoding=encoding) as file:
24
- context = file.read()
25
- return context
26
- except UnicodeDecodeError:
27
- continue
28
- except Exception as e:
29
- return f"An error occurred: {e}"
30
-
31
- return "Error: Unable to read the file with any supported encoding"
 
 
 
 
 
 
 
 
 
32
 
33
  def get_answer(file, question):
34
- if file is None:
35
- return "Please upload a file"
36
- if not question:
37
- return "Please enter a question"
38
-
39
  context = read_file_content(file)
40
- if context.startswith("An error occurred") or context.startswith("Error:"):
41
- return context
42
-
43
- try:
44
- answer = question_answer(question=question, context=context)
45
- return answer["answer"]
46
- except Exception as e:
47
- return f"Error processing question: {str(e)}"
48
 
49
- # Create the Gradio interface
50
- demo = gr.Interface(
51
- fn=get_answer,
52
- inputs=[
53
- gr.File(label="Upload your file (.txt)", type="file"),
54
- gr.Textbox(label="Input your question", lines=1, placeholder="Enter your question here...")
55
- ],
56
- outputs=[
57
- gr.Textbox(label="Answer", lines=2)
58
- ],
59
- title="Document Question & Answer Chatbot",
60
- description="Upload a text document and ask questions about its content. The AI will find relevant answers from the text.",
61
- examples=[
62
- ["sample.txt", "What is the main topic?"]
63
- ]
64
- )
65
 
66
- if __name__ == "__main__":
67
- 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()