elariz commited on
Commit
375082a
1 Parent(s): bcaf184

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """ml-app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1d6MSh5DMmENGXOTWdwsTlwgIqzphq4Pu
8
+ """
9
+
10
+ !pip install gradio
11
+
12
+ import gradio as gr
13
+ from transformers import pipeline
14
+ import re
15
+ import nltk
16
+ nltk.download('punkt')
17
+
18
+ !pip install unidecode
19
+
20
+ from unidecode import unidecode
21
+
22
+ # Preprocessing function: lowercasing and removing special characters
23
+ def preprocess_text(text):
24
+ text = text.lower()
25
+ text = unidecode(text)
26
+ text = re.sub(r'\W+', ' ', text) # Remove non-word characters
27
+ tokens = nltk.word_tokenize(text)
28
+ return ' '.join(tokens) # Returning as a single string instead of tokens for the context
29
+
30
+ # Load the multilingual model for question answering
31
+ qa_model = pipeline("question-answering", model="deepset/xlm-roberta-large-squad2")
32
+
33
+ # Function to generate the answer based on question and uploaded context
34
+ def answer_question(question, context):
35
+ try:
36
+ preprocessed_context = preprocess_text(context)
37
+ result = qa_model(question=question, context=preprocessed_context)
38
+ return result['answer']
39
+ except Exception as e:
40
+ return f"Error: {str(e)}"
41
+
42
+ # Gradio interface
43
+ def qa_app(text_file, question):
44
+ try:
45
+ with open(text_file.name, 'r') as file:
46
+ context = file.read()
47
+ return answer_question(question, context)
48
+ except Exception as e:
49
+ return f"Error reading file: {str(e)}"
50
+
51
+ # Create Gradio interface with updated syntax
52
+ iface = gr.Interface(
53
+ fn=qa_app, # The function that processes input
54
+ inputs=[gr.File(label="Upload your text file"), gr.Textbox(label="Enter your question")],
55
+ outputs="text",
56
+ title="Multilingual Question Answering",
57
+ description="Upload a text file and ask a question based on its content."
58
+ )
59
+
60
+ # Launch the Gradio app
61
+ iface.launch()
62
+