Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +93 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForTokenClassification
|
3 |
+
import torch
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
# Load the models and tokenizers
|
7 |
+
QA_model = AutoModelForQuestionAnswering.from_pretrained("guldasta/xtreme-MLQA.hi.hi")
|
8 |
+
QA_token = AutoTokenizer.from_pretrained("guldasta/xtreme-MLQA.hi.hi")
|
9 |
+
|
10 |
+
Translation_model = AutoModelForSeq2SeqLM.from_pretrained("guldasta/Helsinki-NLP-opus-mt-mul-en-finetuned-hi-to-en")
|
11 |
+
Translation_token = AutoTokenizer.from_pretrained("guldasta/Helsinki-NLP-opus-mt-mul-en-finetuned-hi-to-en")
|
12 |
+
|
13 |
+
TokenClass_Model = AutoModelForTokenClassification.from_pretrained("guldasta/roberta-ner-multilingua-HiNER-Org")
|
14 |
+
TokenClass_Token = AutoTokenizer.from_pretrained("guldasta/roberta-ner-multilingua-HiNER-Org")
|
15 |
+
|
16 |
+
# Define the functions
|
17 |
+
def get_ans(question, context):
|
18 |
+
model_inputs = QA_token(question, context, return_tensors="pt")
|
19 |
+
outputs = QA_model(**model_inputs)
|
20 |
+
start_logits = outputs.start_logits
|
21 |
+
end_logits = outputs.end_logits
|
22 |
+
start_indx = torch.argmax(start_logits)
|
23 |
+
end_indx = torch.argmax(end_logits)
|
24 |
+
answer = QA_token.decode(model_inputs['input_ids'][0][start_indx:end_indx + 1])
|
25 |
+
return answer
|
26 |
+
|
27 |
+
def translate_text(text):
|
28 |
+
model_inputs = Translation_token(text, return_tensors="pt")
|
29 |
+
outputs = Translation_model.generate(**model_inputs)
|
30 |
+
translated_text = Translation_token.decode(outputs[0], skip_special_tokens=True)
|
31 |
+
return translated_text
|
32 |
+
|
33 |
+
def get_pos(text):
|
34 |
+
inputs = TokenClass_Token(text, return_tensors="pt")
|
35 |
+
outputs = TokenClass_Model(**inputs)
|
36 |
+
logits = outputs.logits
|
37 |
+
predictions = torch.argmax(logits, dim=2)
|
38 |
+
tokens = TokenClass_Token.convert_ids_to_tokens(inputs["input_ids"][0])
|
39 |
+
predicted_labels = [TokenClass_Model.config.id2label[label.item()] for label in predictions[0]]
|
40 |
+
|
41 |
+
filtered_tokens = []
|
42 |
+
filtered_labels = []
|
43 |
+
for token, label in zip(tokens, predicted_labels):
|
44 |
+
if token not in TokenClass_Token.all_special_tokens:
|
45 |
+
filtered_tokens.append(token)
|
46 |
+
filtered_labels.append(label)
|
47 |
+
|
48 |
+
df = pd.DataFrame({
|
49 |
+
"Token": filtered_tokens,
|
50 |
+
"Entity": filtered_labels
|
51 |
+
})
|
52 |
+
return df
|
53 |
+
|
54 |
+
# Create Gradio interfaces
|
55 |
+
qa_interface = gr.Interface(
|
56 |
+
fn=get_ans,
|
57 |
+
inputs=[
|
58 |
+
gr.Textbox(lines=5, placeholder="Type a paragraph or context here...", label="Paragraph"),
|
59 |
+
gr.Textbox(lines=1, placeholder="Type your question here...", label="Question"),
|
60 |
+
],
|
61 |
+
outputs=gr.Textbox(label="Answer"),
|
62 |
+
title="Question Answering",
|
63 |
+
description="Enter a paragraph and ask a question to get the answer."
|
64 |
+
)
|
65 |
+
|
66 |
+
translation_interface = gr.Interface(
|
67 |
+
fn=translate_text,
|
68 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your text here...", label="Text to Translate"),
|
69 |
+
outputs=gr.Textbox(label="Translated Text"),
|
70 |
+
title="Text Translation",
|
71 |
+
description="Enter text to translate from Hindi to English."
|
72 |
+
)
|
73 |
+
|
74 |
+
pos_tag_interface = gr.Interface(
|
75 |
+
fn=get_pos,
|
76 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your text here...", label="Text for POS Tagging"),
|
77 |
+
outputs=gr.Dataframe(headers=["Token", "Entity"]),
|
78 |
+
title="POS Tagging",
|
79 |
+
description="Enter text to get POS tags and named entities."
|
80 |
+
)
|
81 |
+
|
82 |
+
# Combine all interfaces into a single Gradio app
|
83 |
+
app = gr.TabbedInterface(
|
84 |
+
[qa_interface, translation_interface, pos_tag_interface],
|
85 |
+
["Question Answering", "Translation", "POS Tagging"]
|
86 |
+
)
|
87 |
+
|
88 |
+
# Launch the app
|
89 |
+
app.launch()
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
pandas
|