hema1 commited on
Commit
2b0923a
1 Parent(s): 48e2dcb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import gradio as gr
3
+ # importing necessary libraries
4
+ from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
5
+
6
+ tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
7
+ model = TFAutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad",return_dict=False)
8
+ from transformers import pipeline
9
+
10
+ nlp = pipeline("question-answering", model=model, tokenizer=tokenizer)
11
+
12
+ context = "My name is Hema Raikhola, i am a data scientist and machine learning engineer."
13
+ question = "what is my profession?"
14
+
15
+ result = nlp(question = question, context=context)
16
+
17
+ print(f"QUESTION: {question}")
18
+ print(f"ANSWER: {result['answer']}")
19
+
20
+ # creating the function
21
+ def func(context, question):
22
+ result = nlp(question = question, context=context)
23
+ return result['answer']
24
+
25
+ example_1 = "(1) Kanisha,Preeti,Hema and Shaksham are the team members.They are working on a machine learning project"
26
+ qst_1 = "who are the team members?"
27
+
28
+ example_2 = "(2) Natural Language Processing (NLP) allows machines to break down and interpret human language. It's at the core of tools we use every day – from translation software, chatbots, spam filters, and search engines, to grammar correction software, voice assistants, and social media monitoring tools."
29
+ qst_2 = "What is NLP used for?"
30
+
31
+
32
+ from transformers import ViltProcessor, ViltForQuestionAnswering
33
+
34
+
35
+ def getResult(query, image):
36
+ # prepare image + question
37
+ #image = Image.open(BytesIO(base64.b64decode(base64_encoded_image)))
38
+ text = query
39
+
40
+ processor = ViltProcessor.from_pretrained(
41
+ "dandelin/vilt-b32-finetuned-vqa")
42
+ model = ViltForQuestionAnswering.from_pretrained(
43
+ "dandelin/vilt-b32-finetuned-vqa")
44
+
45
+ # prepare inputs
46
+ encoding = processor(image, text, return_tensors="pt")
47
+
48
+ # forward pass
49
+ outputs = model(**encoding)
50
+ logits = outputs.logits
51
+ idx = logits.argmax(-1).item()
52
+ print("Predicted answer:", model.config.id2label[idx])
53
+ return model.config.id2label[idx]
54
+
55
+ # creating the interface
56
+ iface = gr.Interface(fn=getResult, inputs=[
57
+ "text", gr.Image(type="pil")], outputs="text")
58
+
59
+ # creating the interface
60
+ app = gr.Interface(fn=func,
61
+ inputs = ['textbox', 'text'],
62
+ outputs = gr.Textbox( lines=10),
63
+ title = 'Question Answering bot',
64
+ description = 'Input context and question, then get answers!',
65
+ examples = [[example_1, qst_1],
66
+ [example_2, qst_2]],
67
+ theme = "darkhuggingface",
68
+ Timeout =120,
69
+ allow_flagging="manual",
70
+ flagging_options=["incorrect", "ambiguous", "offensive", "other"],
71
+
72
+ ).queue()
73
+ # launching the app
74
+ gr.TabbedInterface([iface,app],["Visual QA","Text QA"]).launch(auth = ('user','teamwork'), auth_message = "Check your Login details sent to your email")