Spaces:
No application file
No application file
Initial commit
Browse files- .gradio/certificate.pem +31 -0
- .idea/.gitignore +5 -0
- .idea/PT.iml +10 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/modules.xml +8 -0
- PT.py +164 -0
- Requirements.txt.txt +3 -0
- language.json +26 -0
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mUgjU+g4rk8Kb4Mu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wTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
|
| 25 |
+
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
|
| 26 |
+
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
|
| 27 |
+
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
|
| 28 |
+
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
|
| 29 |
+
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
|
| 30 |
+
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
|
| 31 |
+
-----END CERTIFICATE-----
|
.idea/.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
.idea/PT.iml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$">
|
| 5 |
+
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
| 6 |
+
</content>
|
| 7 |
+
<orderEntry type="jdk" jdkName="Python 3.13 (PT)" jdkType="Python SDK" />
|
| 8 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 9 |
+
</component>
|
| 10 |
+
</module>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/PT.iml" filepath="$PROJECT_DIR$/.idea/PT.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
PT.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
from transformers import pipeline, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
print("Loading Summarization model: t5-base...")
|
| 7 |
+
text_summary = pipeline(
|
| 8 |
+
"summarization",
|
| 9 |
+
model="t5-base",
|
| 10 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 11 |
+
)
|
| 12 |
+
print("Summarization model loaded.")
|
| 13 |
+
|
| 14 |
+
def summarize_text(input_text):
|
| 15 |
+
output = text_summary(input_text, min_length=30, max_length=120)
|
| 16 |
+
return output[0]['summary_text']
|
| 17 |
+
|
| 18 |
+
print("Loading Translation model: facebook/nllb-200-distilled-600M...")
|
| 19 |
+
text_translator = pipeline(
|
| 20 |
+
"translation",
|
| 21 |
+
model="facebook/nllb-200-distilled-600M",
|
| 22 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 23 |
+
)
|
| 24 |
+
print("Translation model loaded.")
|
| 25 |
+
|
| 26 |
+
with open('language.json', 'r', encoding='utf-8') as file:
|
| 27 |
+
language_data = json.load(file)
|
| 28 |
+
|
| 29 |
+
def get_FLORES_code_from_language(language_name):
|
| 30 |
+
for entry in language_data:
|
| 31 |
+
if entry['Language'].lower() == language_name.lower():
|
| 32 |
+
return entry['FLORES-200 code']
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def translate_text(text, destination_language_name):
|
| 36 |
+
dest_code = get_FLORES_code_from_language(destination_language_name)
|
| 37 |
+
|
| 38 |
+
if not dest_code:
|
| 39 |
+
return f"Error: FLORES-200 code not found for '{destination_language_name}'. Please select a language from the list."
|
| 40 |
+
|
| 41 |
+
print(f"Translating from English (eng_Latn) to {destination_language_name} ({dest_code})...")
|
| 42 |
+
|
| 43 |
+
translation = text_translator(
|
| 44 |
+
text,
|
| 45 |
+
src_lang="eng_Latn",
|
| 46 |
+
tgt_lang=dest_code
|
| 47 |
+
)
|
| 48 |
+
return translation[0]["translation_text"]
|
| 49 |
+
|
| 50 |
+
print("Loading Question-Answering model: distilbert-base-uncased-distilled-squad...")
|
| 51 |
+
Youtube = pipeline(
|
| 52 |
+
"question-answering",
|
| 53 |
+
model="distilbert-base-uncased-distilled-squad",
|
| 54 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 55 |
+
)
|
| 56 |
+
print("Question-Answering model loaded.")
|
| 57 |
+
|
| 58 |
+
def read_file_content(file_obj):
|
| 59 |
+
try:
|
| 60 |
+
with open(file_obj.name, 'r', encoding='utf-8') as file:
|
| 61 |
+
return file.read()
|
| 62 |
+
except UnicodeDecodeError:
|
| 63 |
+
try:
|
| 64 |
+
with open(file_obj.name, 'r', encoding='latin-1') as file:
|
| 65 |
+
return file.read()
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"An error occurred: {e}"
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"An error occurred: {e}"
|
| 70 |
+
|
| 71 |
+
def get_answer(file, question):
|
| 72 |
+
context = read_file_content(file)
|
| 73 |
+
if "An error occurred" in context:
|
| 74 |
+
return context
|
| 75 |
+
answer = Youtube(question=question, context=context)
|
| 76 |
+
if answer['score'] < 0.2:
|
| 77 |
+
return "I am not confident I can answer this question based on the provided text."
|
| 78 |
+
return answer["answer"]
|
| 79 |
+
|
| 80 |
+
def landing_page():
|
| 81 |
+
return (
|
| 82 |
+
"<div style='max-width:900px; margin:36px auto 0 auto; background: linear-gradient(135deg, #eef6fb 0%, #e0ecfa 100%); "
|
| 83 |
+
"border-radius: 30px; box-shadow:0 8px 36px #b6c4e033; padding: 60px 0 45px 0; text-align:center;'>"
|
| 84 |
+
"<h1 style='font-size:3.2em; color:#174ea6; font-weight:900; letter-spacing:-1.5px; margin-bottom:10px;'>Unified NLP Toolkit ✨</h1>"
|
| 85 |
+
"<p style='font-size:1.27em; color:#51627a; margin:24px 0 27px 0;'>"
|
| 86 |
+
"Summarize, translate, or ask questions on documents using a modern, friendly interface.<br><br>"
|
| 87 |
+
"Select a tool below to get started."
|
| 88 |
+
"</p>"
|
| 89 |
+
"<div style='display:flex; flex-wrap:wrap; justify-content:center; gap:30px; margin-top:27px;'>"
|
| 90 |
+
"<a href='#summarizer' style='background:linear-gradient(100deg,#2563eb 70%,#4f92fc 100%) !important; "
|
| 91 |
+
"color:white; padding:17px 36px 17px 36px; border-radius:16px; text-decoration:none; font-size:1.16em; font-weight:700; "
|
| 92 |
+
"transition: box-shadow .15s; box-shadow:0 4px 14px #2563eb22; outline:none;'>Text Summarizer</a>"
|
| 93 |
+
"<a href='#translator' style='background:linear-gradient(100deg,#059669 70%,#34d399 110%) !important; "
|
| 94 |
+
"color:white; padding:17px 36px 17px 36px; border-radius:16px; text-decoration:none; font-size:1.16em; font-weight:700; "
|
| 95 |
+
"transition: box-shadow .15s; box-shadow:0 4px 14px #05966922; outline:none;'>Translator</a>"
|
| 96 |
+
"<a href='#qna' style='background:linear-gradient(100deg,#f59e42 70%,#fbbf24 110%) !important; "
|
| 97 |
+
"color:white; padding:17px 36px 17px 36px; border-radius:16px; text-decoration:none; font-size:1.16em; font-weight:700; "
|
| 98 |
+
"transition: box-shadow .15s; box-shadow:0 4px 14px #f59e4222; outline:none;'>Document Q&A</a>"
|
| 99 |
+
"</div>"
|
| 100 |
+
"</div>"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
def footer():
|
| 104 |
+
return (
|
| 105 |
+
"<div style='text-align:center; margin-top:54px; margin-bottom:12px; color:#64748b; font-size:1.13em; letter-spacing:0.5px;'>"
|
| 106 |
+
"Made with <span style='color:#e11d48;'>❤️</span> by tushar"
|
| 107 |
+
"</div>"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Blocks(title="Unified NLP Toolkit ✨", theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate")) as demo:
|
| 111 |
+
gr.HTML(landing_page())
|
| 112 |
+
|
| 113 |
+
with gr.Tab("Text Summarizer", elem_id="summarizer"):
|
| 114 |
+
gr.Markdown("### Text Summarizer")
|
| 115 |
+
gr.Markdown("> Enter your text below and click **Summarize** to generate a concise summary.")
|
| 116 |
+
inp = gr.Textbox(
|
| 117 |
+
label="Input",
|
| 118 |
+
lines=10,
|
| 119 |
+
placeholder="Paste your text here..."
|
| 120 |
+
)
|
| 121 |
+
btn = gr.Button(
|
| 122 |
+
"Summarize",
|
| 123 |
+
elem_id="summarize-btn",
|
| 124 |
+
variant="secondary"
|
| 125 |
+
)
|
| 126 |
+
out = gr.Textbox(label="Summary", lines=4)
|
| 127 |
+
btn.click(fn=summarize_text, inputs=inp, outputs=out)
|
| 128 |
+
|
| 129 |
+
with gr.Tab("Multilanguage Translator", elem_id="translator"):
|
| 130 |
+
gr.Markdown("### Multilanguage Translator")
|
| 131 |
+
gr.Markdown("> Enter English text, select an Indian language, and click **Translate**.")
|
| 132 |
+
inp3 = gr.Textbox(
|
| 133 |
+
label="English Text",
|
| 134 |
+
lines=10
|
| 135 |
+
)
|
| 136 |
+
lang_options = [entry['Language'] for entry in language_data]
|
| 137 |
+
lang_dropdown = gr.Dropdown(
|
| 138 |
+
lang_options,
|
| 139 |
+
label="Select Indian Language"
|
| 140 |
+
)
|
| 141 |
+
btn3 = gr.Button(
|
| 142 |
+
"Translate",
|
| 143 |
+
elem_id="translate-btn",
|
| 144 |
+
variant="secondary"
|
| 145 |
+
)
|
| 146 |
+
out3 = gr.Textbox(label="Translated Text", lines=4)
|
| 147 |
+
btn3.click(fn=translate_text, inputs=[inp3, lang_dropdown], outputs=out3)
|
| 148 |
+
|
| 149 |
+
with gr.Tab("Document QnA", elem_id="qna"):
|
| 150 |
+
gr.Markdown("### Document Q&A")
|
| 151 |
+
gr.Markdown("> Upload a `.txt` file and ask a question about its content.")
|
| 152 |
+
file_inp = gr.File(label="Upload Text File")
|
| 153 |
+
q_inp = gr.Textbox(label="Your Question", lines=2)
|
| 154 |
+
btn4 = gr.Button(
|
| 155 |
+
"Get Answer",
|
| 156 |
+
elem_id="qna-btn",
|
| 157 |
+
variant="secondary"
|
| 158 |
+
)
|
| 159 |
+
out4 = gr.Textbox(label="Answer", lines=2)
|
| 160 |
+
btn4.click(fn=get_answer, inputs=[file_inp, q_inp], outputs=out4)
|
| 161 |
+
|
| 162 |
+
gr.HTML(footer())
|
| 163 |
+
|
| 164 |
+
demo.launch()
|
Requirements.txt.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|
language.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{"Language": "Assamese", "FLORES-200 code": "asm_Beng"},
|
| 3 |
+
{"Language": "Awadhi", "FLORES-200 code": "awa_Deva"},
|
| 4 |
+
{"Language": "Bengali", "FLORES-200 code": "ben_Beng"},
|
| 5 |
+
{"Language": "Bhojpuri", "FLORES-200 code": "bho_Deva"},
|
| 6 |
+
{"Language": "Dogri", "FLORES-200 code": "dgo_Deva"},
|
| 7 |
+
{"Language": "Gujarati", "FLORES-200 code": "guj_Gujr"},
|
| 8 |
+
{"Language": "Hindi", "FLORES-200 code": "hin_Deva"},
|
| 9 |
+
{"Language": "Kannada", "FLORES-200 code": "kan_Knda"},
|
| 10 |
+
{"Language": "Kashmiri", "FLORES-200 code": "kas_Arab"},
|
| 11 |
+
{"Language": "Konkani", "FLORES-200 code": "kok_Deva"},
|
| 12 |
+
{"Language": "Maithili", "FLORES-200 code": "mai_Deva"},
|
| 13 |
+
{"Language": "Malayalam", "FLORES-200 code": "mal_Mlym"},
|
| 14 |
+
{"Language": "Marathi", "FLORES-200 code": "mar_Deva"},
|
| 15 |
+
{"Language": "Meitei (Bengali)", "FLORES-200 code": "mni_Beng"},
|
| 16 |
+
{"Language": "Mizo", "FLORES-200 code": "lus_Latn"},
|
| 17 |
+
{"Language": "Nepali", "FLORES-200 code": "npi_Deva"},
|
| 18 |
+
{"Language": "Oriya", "FLORES-200 code": "ory_Orya"},
|
| 19 |
+
{"Language": "Punjabi (Gurmukhi)", "FLORES-200 code": "pan_Guru"},
|
| 20 |
+
{"Language": "Sanskrit", "FLORES-200 code": "san_Deva"},
|
| 21 |
+
{"Language": "Santali", "FLORES-200 code": "sat_Olck"},
|
| 22 |
+
{"Language": "Sindhi", "FLORES-200 code": "snd_Arab"},
|
| 23 |
+
{"Language": "Tamil", "FLORES-200 code": "tam_Taml"},
|
| 24 |
+
{"Language": "Telugu", "FLORES-200 code": "tel_Telu"},
|
| 25 |
+
{"Language": "Urdu", "FLORES-200 code": "urd_Arab"}
|
| 26 |
+
]
|