Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
ner_model = pipeline("ner", grouped_entities=True)
|
|
|
|
|
|
|
| 8 |
fill_mask = pipeline("fill-mask", model="roberta-large")
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def classify_environment(text):
|
| 12 |
result = classifier_env(text)
|
| 13 |
return result[0]['label'], result[0]['score']
|
| 14 |
|
| 15 |
def generate_image(prompt):
|
| 16 |
-
|
| 17 |
-
return
|
| 18 |
|
| 19 |
def ner_mapping(text):
|
| 20 |
entities = ner_model(text)
|
|
@@ -24,9 +40,12 @@ def fill_masked_text(text):
|
|
| 24 |
results = fill_mask(text)
|
| 25 |
return "\n".join([r['sequence'] for r in results])
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
-
gr.Markdown("#
|
| 30 |
|
| 31 |
with gr.Tab("Sentence Classification (Environment)"):
|
| 32 |
input_text = gr.Textbox(placeholder="Type a sentence about the environment...", lines=2)
|
|
@@ -53,5 +72,4 @@ with gr.Blocks() as demo:
|
|
| 53 |
mask_btn = gr.Button("Fill Mask")
|
| 54 |
mask_btn.click(fn=fill_masked_text, inputs=mask_input, outputs=mask_output)
|
| 55 |
|
| 56 |
-
# Launch
|
| 57 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from diffusers import StableDiffusionPipeline
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# ==============================
|
| 7 |
+
# Load Pipelines
|
| 8 |
+
# ==============================
|
| 9 |
+
|
| 10 |
+
# Sentence classification (use a multi-class model if available)
|
| 11 |
+
classifier_env = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion") # You can fine-tune your own later
|
| 12 |
+
|
| 13 |
+
# NER pipeline
|
| 14 |
ner_model = pipeline("ner", grouped_entities=True)
|
| 15 |
+
|
| 16 |
+
# Masked word fill
|
| 17 |
fill_mask = pipeline("fill-mask", model="roberta-large")
|
| 18 |
|
| 19 |
+
# Stable Diffusion for image generation
|
| 20 |
+
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float32)
|
| 21 |
+
pipe = pipe.to("cpu")
|
| 22 |
+
|
| 23 |
+
# ==============================
|
| 24 |
+
# Define Functions
|
| 25 |
+
# ==============================
|
| 26 |
+
|
| 27 |
def classify_environment(text):
|
| 28 |
result = classifier_env(text)
|
| 29 |
return result[0]['label'], result[0]['score']
|
| 30 |
|
| 31 |
def generate_image(prompt):
|
| 32 |
+
image = pipe(prompt).images[0]
|
| 33 |
+
return image
|
| 34 |
|
| 35 |
def ner_mapping(text):
|
| 36 |
entities = ner_model(text)
|
|
|
|
| 40 |
results = fill_mask(text)
|
| 41 |
return "\n".join([r['sequence'] for r in results])
|
| 42 |
|
| 43 |
+
# ==============================
|
| 44 |
+
# Gradio Interface
|
| 45 |
+
# ==============================
|
| 46 |
+
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("# 🤖 Multi-Model NLP + Image Generator (Hugging Face Space)")
|
| 49 |
|
| 50 |
with gr.Tab("Sentence Classification (Environment)"):
|
| 51 |
input_text = gr.Textbox(placeholder="Type a sentence about the environment...", lines=2)
|
|
|
|
| 72 |
mask_btn = gr.Button("Fill Mask")
|
| 73 |
mask_btn.click(fn=fill_masked_text, inputs=mask_input, outputs=mask_output)
|
| 74 |
|
|
|
|
| 75 |
demo.launch()
|