File size: 1,866 Bytes
375082a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
"""ml-app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1d6MSh5DMmENGXOTWdwsTlwgIqzphq4Pu
"""

!pip install gradio

import gradio as gr
from transformers import pipeline
import re
import nltk
nltk.download('punkt')

!pip install unidecode

from unidecode import unidecode

# Preprocessing function: lowercasing and removing special characters
def preprocess_text(text):
    text = text.lower()
    text = unidecode(text)
    text = re.sub(r'\W+', ' ', text)  # Remove non-word characters
    tokens = nltk.word_tokenize(text)
    return ' '.join(tokens)  # Returning as a single string instead of tokens for the context

# Load the multilingual model for question answering
qa_model = pipeline("question-answering", model="deepset/xlm-roberta-large-squad2")

# Function to generate the answer based on question and uploaded context
def answer_question(question, context):
    try:
        preprocessed_context = preprocess_text(context)
        result = qa_model(question=question, context=preprocessed_context)
        return result['answer']
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio interface
def qa_app(text_file, question):
    try:
        with open(text_file.name, 'r') as file:
            context = file.read()
        return answer_question(question, context)
    except Exception as e:
        return f"Error reading file: {str(e)}"

# Create Gradio interface with updated syntax
iface = gr.Interface(
    fn=qa_app,  # The function that processes input
    inputs=[gr.File(label="Upload your text file"), gr.Textbox(label="Enter your question")],
    outputs="text",
    title="Multilingual Question Answering",
    description="Upload a text file and ask a question based on its content."
)

# Launch the Gradio app
iface.launch()