Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForQuestionAnswering
|
4 |
+
|
5 |
+
# Load Translation Model
|
6 |
+
translation_model_name = "VietAI/envit5-translation"
|
7 |
+
translation_tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
|
8 |
+
translation_model = AutoModelForSeq2SeqLM.from_pretrained(translation_model_name)
|
9 |
+
|
10 |
+
# Translation Function
|
11 |
+
def translate_text(text, source_lang, target_lang):
|
12 |
+
prompt = f"Translate the following text from {source_lang} to {target_lang}: {text}"
|
13 |
+
inputs = translation_tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
|
14 |
+
|
15 |
+
with torch.no_grad():
|
16 |
+
output = translation_model.generate(**inputs, max_length=256)
|
17 |
+
|
18 |
+
return translation_tokenizer.decode(output[0], skip_special_tokens=True)
|
19 |
+
|
20 |
+
# Load Question Answering Model
|
21 |
+
qa_model_name = "atharvamundada99/bert-large-question-answering-finetuned-legal"
|
22 |
+
qa_tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
|
23 |
+
qa_model = AutoModelForQuestionAnswering.from_pretrained(qa_model_name)
|
24 |
+
|
25 |
+
# Question Answering Function
|
26 |
+
def answer_question(question, context):
|
27 |
+
inputs = qa_tokenizer(question, context, return_tensors="pt", truncation=True)
|
28 |
+
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = qa_model(**inputs)
|
31 |
+
|
32 |
+
answer_start = torch.argmax(outputs.start_logits)
|
33 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
34 |
+
answer = qa_tokenizer.convert_tokens_to_string(
|
35 |
+
qa_tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end])
|
36 |
+
)
|
37 |
+
|
38 |
+
return answer if answer.strip() else "Sorry, I couldn't find a relevant answer."
|
39 |
+
|
40 |
+
# Load Summarization Model
|
41 |
+
summarization_model_name = "Falconsai/medical_summarization"
|
42 |
+
summarization_tokenizer = AutoTokenizer.from_pretrained(summarization_model_name)
|
43 |
+
summarization_model = AutoModelForSeq2SeqLM.from_pretrained(summarization_model_name)
|
44 |
+
|
45 |
+
# Summarization Function
|
46 |
+
def summarize_text(text):
|
47 |
+
inputs = summarization_tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
48 |
+
with torch.no_grad():
|
49 |
+
summary_ids = summarization_model.generate(**inputs, max_length=150, min_length=50, length_penalty=2.0, num_beams=4)
|
50 |
+
|
51 |
+
return summarization_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
52 |
+
|
53 |
+
# Function to toggle UI visibility based on selected task
|
54 |
+
def select_task(task):
|
55 |
+
return (
|
56 |
+
gr.update(visible=(task == "Translation")),
|
57 |
+
gr.update(visible=(task == "Question Answering")),
|
58 |
+
gr.update(visible=(task == "Summarization")),
|
59 |
+
)
|
60 |
+
|
61 |
+
# Function to clear inputs and outputs
|
62 |
+
def clear_fields():
|
63 |
+
return "", "", "", ""
|
64 |
+
|
65 |
+
def clear_fields_summary():
|
66 |
+
return ""
|
67 |
+
|
68 |
+
# Gradio UI
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("## AI-Powered Language Processing")
|
71 |
+
|
72 |
+
task_buttons = gr.Radio(["Translation", "Question Answering", "Summarization"], label="Choose a task")
|
73 |
+
|
74 |
+
with gr.Group(visible=False) as translation_ui:
|
75 |
+
source_lang = gr.Textbox(label="Source Language")
|
76 |
+
target_lang = gr.Textbox(label="Target Language")
|
77 |
+
text_input = gr.Textbox(label="Enter Text")
|
78 |
+
translate_button = gr.Button("Translate")
|
79 |
+
translation_output = gr.Textbox(label="Translated Text")
|
80 |
+
|
81 |
+
clear_button_t = gr.Button("Clear")
|
82 |
+
clear_button_t.click(clear_fields, inputs=[], outputs=[source_lang, target_lang, text_input, translation_output])
|
83 |
+
|
84 |
+
translate_button.click(translate_text, inputs=[text_input, source_lang, target_lang], outputs=translation_output)
|
85 |
+
|
86 |
+
with gr.Group(visible=False) as qa_ui:
|
87 |
+
question_input = gr.Textbox(label="Enter Question")
|
88 |
+
context_input = gr.Textbox(label="Enter Context")
|
89 |
+
answer_button = gr.Button("Get Answer")
|
90 |
+
qa_output = gr.Textbox(label="Answer")
|
91 |
+
|
92 |
+
clear_button_qa = gr.Button("Clear")
|
93 |
+
clear_button_qa.click(clear_fields, inputs=[], outputs=[question_input, context_input, qa_output])
|
94 |
+
|
95 |
+
answer_button.click(answer_question, inputs=[question_input, context_input], outputs=qa_output)
|
96 |
+
|
97 |
+
with gr.Group(visible=False) as summarization_ui:
|
98 |
+
text_input_summary = gr.Textbox(label="Enter Text")
|
99 |
+
summarize_button = gr.Button("Summarize")
|
100 |
+
summary_output = gr.Textbox(label="Summary")
|
101 |
+
|
102 |
+
clear_button_s = gr.Button("Clear")
|
103 |
+
clear_button_s.click(clear_fields_summary, inputs=[], outputs=[text_input_summary, summary_output])
|
104 |
+
|
105 |
+
summarize_button.click(summarize_text, inputs=[text_input_summary], outputs=summary_output)
|
106 |
+
|
107 |
+
task_buttons.change(select_task, inputs=[task_buttons], outputs=[translation_ui, qa_ui, summarization_ui])
|
108 |
+
|
109 |
+
demo.launch(share=True)
|