elariz commited on
Commit
6e45a4d
·
verified ·
1 Parent(s): d4fda7c

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
+ """app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1740ajnCP_JU3oCXfVpmvKcaHdtlYWp9S
8
+ """
9
+
10
+ !pip install gradio
11
+ !pip install transformers
12
+ !pip install spacy
13
+ import gradio as gr
14
+ import re
15
+ import spacy
16
+ from transformers import pipeline
17
+
18
+ !pip install unidecode
19
+
20
+ from unidecode import unidecode
21
+
22
+ # Load spaCy's English model
23
+ nlp = spacy.load("en_core_web_sm")
24
+
25
+ def preprocess_text(text):
26
+ doc = nlp(text.lower()) # Tokenize and lowercase the text
27
+ tokens = [token.text for token in doc if not token.is_punct] # Remove punctuation
28
+ return tokens
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=" ".join(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
+