Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Use CPU on Hugging Face Spaces free tier
|
| 6 |
DEVICE = -1
|
| 7 |
-
|
| 8 |
-
# Lazy loading (loads model only when first used)
|
| 9 |
models = {}
|
| 10 |
|
| 11 |
def get_model(task_name):
|
| 12 |
if task_name not in models:
|
|
|
|
| 13 |
if task_name == "Chatbot":
|
| 14 |
models[task_name] = pipeline(
|
| 15 |
"text-generation",
|
|
@@ -46,6 +43,13 @@ def get_model(task_name):
|
|
| 46 |
device=DEVICE
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return models[task_name]
|
| 50 |
|
| 51 |
|
|
@@ -62,8 +66,7 @@ def run_task(task, user_input, chat_history):
|
|
| 62 |
max_new_tokens=100,
|
| 63 |
pad_token_id=50256
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
bot_reply = response[0]["generated_text"]
|
| 67 |
chat_history = chat_history + [(user_input, bot_reply)]
|
| 68 |
return "", chat_history
|
| 69 |
|
|
@@ -85,7 +88,6 @@ def run_task(task, user_input, chat_history):
|
|
| 85 |
entities = model(user_input)
|
| 86 |
if not entities:
|
| 87 |
return "No entities found.", chat_history
|
| 88 |
-
|
| 89 |
formatted = "\n".join(
|
| 90 |
f"{e['word']} ({e['entity_group']}) - {e['score']:.2f}"
|
| 91 |
for e in entities
|
|
@@ -96,6 +98,16 @@ def run_task(task, user_input, chat_history):
|
|
| 96 |
translation = model(user_input)[0]["translation_text"]
|
| 97 |
return translation, chat_history
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
with gr.Blocks(title="NLP Application") as demo:
|
| 101 |
|
|
@@ -107,7 +119,8 @@ with gr.Blocks(title="NLP Application") as demo:
|
|
| 107 |
"Sentiment Analysis",
|
| 108 |
"NER",
|
| 109 |
"Summarization",
|
| 110 |
-
"Translation (EN→FR)"
|
|
|
|
| 111 |
],
|
| 112 |
label="Select NLP Task"
|
| 113 |
)
|
|
@@ -118,9 +131,7 @@ with gr.Blocks(title="NLP Application") as demo:
|
|
| 118 |
label="Input Text"
|
| 119 |
)
|
| 120 |
|
| 121 |
-
output_box = gr.Textbox(
|
| 122 |
-
label="Output"
|
| 123 |
-
)
|
| 124 |
|
| 125 |
chatbot = gr.Chatbot(label="Conversation")
|
| 126 |
|
|
@@ -134,4 +145,6 @@ with gr.Blocks(title="NLP Application") as demo:
|
|
| 134 |
outputs=[output_box, chatbot]
|
| 135 |
)
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
|
|
|
| 4 |
DEVICE = -1
|
|
|
|
|
|
|
| 5 |
models = {}
|
| 6 |
|
| 7 |
def get_model(task_name):
|
| 8 |
if task_name not in models:
|
| 9 |
+
|
| 10 |
if task_name == "Chatbot":
|
| 11 |
models[task_name] = pipeline(
|
| 12 |
"text-generation",
|
|
|
|
| 43 |
device=DEVICE
|
| 44 |
)
|
| 45 |
|
| 46 |
+
elif task_name == "Fill Mask":
|
| 47 |
+
models[task_name] = pipeline(
|
| 48 |
+
"fill-mask",
|
| 49 |
+
model="bert-base-uncased",
|
| 50 |
+
device=DEVICE
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
return models[task_name]
|
| 54 |
|
| 55 |
|
|
|
|
| 66 |
max_new_tokens=100,
|
| 67 |
pad_token_id=50256
|
| 68 |
)
|
| 69 |
+
bot_reply = response[0]["generated_text"][len(user_input):]
|
|
|
|
| 70 |
chat_history = chat_history + [(user_input, bot_reply)]
|
| 71 |
return "", chat_history
|
| 72 |
|
|
|
|
| 88 |
entities = model(user_input)
|
| 89 |
if not entities:
|
| 90 |
return "No entities found.", chat_history
|
|
|
|
| 91 |
formatted = "\n".join(
|
| 92 |
f"{e['word']} ({e['entity_group']}) - {e['score']:.2f}"
|
| 93 |
for e in entities
|
|
|
|
| 98 |
translation = model(user_input)[0]["translation_text"]
|
| 99 |
return translation, chat_history
|
| 100 |
|
| 101 |
+
elif task == "Fill Mask":
|
| 102 |
+
if "<mask>" not in user_input:
|
| 103 |
+
return "Please include the token <mask> in your sentence.", chat_history
|
| 104 |
+
predictions = model(user_input)
|
| 105 |
+
formatted = "\n".join(
|
| 106 |
+
f"{p['token_str']} (score: {p['score']:.4f})"
|
| 107 |
+
for p in predictions
|
| 108 |
+
)
|
| 109 |
+
return formatted, chat_history
|
| 110 |
+
|
| 111 |
|
| 112 |
with gr.Blocks(title="NLP Application") as demo:
|
| 113 |
|
|
|
|
| 119 |
"Sentiment Analysis",
|
| 120 |
"NER",
|
| 121 |
"Summarization",
|
| 122 |
+
"Translation (EN→FR)",
|
| 123 |
+
"Fill Mask"
|
| 124 |
],
|
| 125 |
label="Select NLP Task"
|
| 126 |
)
|
|
|
|
| 131 |
label="Input Text"
|
| 132 |
)
|
| 133 |
|
| 134 |
+
output_box = gr.Textbox(label="Output")
|
|
|
|
|
|
|
| 135 |
|
| 136 |
chatbot = gr.Chatbot(label="Conversation")
|
| 137 |
|
|
|
|
| 145 |
outputs=[output_box, chatbot]
|
| 146 |
)
|
| 147 |
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
demo.launch()
|