arjunvankani commited on
Commit
24f43bf
Β·
verified Β·
1 Parent(s): 7774178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -18
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
 
5
 
6
  # --- Load NLP pipelines ---
7
  clf = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
@@ -15,16 +16,33 @@ det = pipeline("object-detection", model="facebook/detr-resnet-50")
15
  seg = pipeline("image-segmentation", model="facebook/mask2former-swin-base-coco-instance")
16
 
17
  # --- Diffusion model for text-to-image ---
18
- sd_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
 
 
19
  sd_pipe = sd_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
20
 
 
 
 
 
 
 
 
 
21
 
22
  # --- Functions ---
23
  def classify_text(text):
24
- return clf(text)
 
 
 
 
 
 
25
 
26
  def ner_text(text):
27
- return ner(text)
 
28
 
29
  def fill_blank(text):
30
  return mlm(text)
@@ -45,52 +63,83 @@ def generate_image(prompt):
45
  image = sd_pipe(prompt).images[0]
46
  return image
47
 
48
-
49
-
50
  # --- Gradio Interface ---
51
- with gr.Blocks() as demo:
52
- gr.Markdown("# 🌍 Environmental AI Toolkit")
53
-
54
- with gr.Tab("Sentence Classification"):
55
- txt_in = gr.Textbox(label="Enter text")
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  txt_out = gr.JSON(label="Classification Result")
 
57
  txt_in.submit(classify_text, txt_in, txt_out)
 
58
 
59
- with gr.Tab("NER"):
 
 
 
 
60
  ner_in = gr.Textbox(label="Enter text")
61
  ner_out = gr.JSON(label="Entities")
62
  ner_in.submit(ner_text, ner_in, ner_out)
63
 
64
- with gr.Tab("Fill-in-the-Blank"):
 
 
 
 
65
  mlm_in = gr.Textbox(label="Enter sentence with [MASK]")
66
  mlm_out = gr.JSON(label="Predictions")
67
  mlm_in.submit(fill_blank, mlm_in, mlm_out)
68
 
69
- with gr.Tab("Question Answering"):
 
 
 
 
70
  context = gr.Textbox(label="Context")
71
  question = gr.Textbox(label="Question")
72
  qa_out = gr.JSON(label="Answer")
73
  gr.Button("Answer").click(answer_question, [context, question], qa_out)
74
 
75
- with gr.Tab("Image Classification"):
 
76
  img_in = gr.Image(type="pil")
77
  img_out = gr.JSON(label="Labels")
78
  img_in.upload(classify_image, img_in, img_out)
79
 
80
- with gr.Tab("Object Detection"):
 
81
  det_in = gr.Image(type="pil")
82
  det_out = gr.JSON(label="Objects")
83
  det_in.upload(detect_objects, det_in, det_out)
84
 
85
- with gr.Tab("Segmentation"):
 
86
  seg_in = gr.Image(type="pil")
87
  seg_out = gr.JSON(label="Segments")
88
  seg_in.upload(segment_image, seg_in, seg_out)
89
 
90
- with gr.Tab("Image Generation"):
 
 
 
 
91
  gen_in = gr.Textbox(label="Prompt")
92
  gen_out = gr.Image(label="Generated Image")
93
  gr.Button("Generate").click(generate_image, gen_in, gen_out)
94
 
95
-
96
  demo.launch()
 
2
  from transformers import pipeline
3
  from diffusers import StableDiffusionPipeline
4
  import torch
5
+ from PIL import Image
6
 
7
  # --- Load NLP pipelines ---
8
  clf = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
 
16
  seg = pipeline("image-segmentation", model="facebook/mask2former-swin-base-coco-instance")
17
 
18
  # --- Diffusion model for text-to-image ---
19
+ sd_pipe = StableDiffusionPipeline.from_pretrained(
20
+ "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
21
+ )
22
  sd_pipe = sd_pipe.to("cuda" if torch.cuda.is_available() else "cpu")
23
 
24
+ # --- Sample sentences ---
25
+ SAMPLE_SENTENCES = [
26
+ "The Amazon rainforest is losing trees at an alarming rate due to deforestation.",
27
+ "Ocean pollution is harming marine life around the world.",
28
+ "Renewable energy like solar and wind can reduce global warming.",
29
+ "Wildlife conservation is essential to protect endangered species.",
30
+ "Sustainable farming practices improve soil health and reduce pollution."
31
+ ]
32
 
33
  # --- Functions ---
34
  def classify_text(text):
35
+ result = clf(text)[0]
36
+ label = result['label']
37
+ score = result['score']
38
+ # Map to Positive/Negative/Neutral
39
+ sentiment_map = {"POSITIVE": "Positive 😊", "NEGATIVE": "Negative 😟"}
40
+ sentiment = sentiment_map.get(label, "Neutral 😐")
41
+ return {"Sentiment": sentiment, "Confidence": round(score, 3)}
42
 
43
  def ner_text(text):
44
+ entities = ner(text)
45
+ return entities
46
 
47
  def fill_blank(text):
48
  return mlm(text)
 
63
  image = sd_pipe(prompt).images[0]
64
  return image
65
 
 
 
66
  # --- Gradio Interface ---
67
+ with gr.Blocks(title="🌍 Environmental AI Toolkit") as demo:
68
+ gr.Markdown("""
69
+ # 🌱 Environmental AI Toolkit
70
+ This toolkit provides multiple AI-powered tools for environmental text and image analysis:
71
+ - **Sentence Classification**: Analyze environmental text sentiment (Positive/Negative)
72
+ - **NER**: Identify environmental entities in text
73
+ - **Fill-in-the-Blank**: Complete environmental sentences using AI
74
+ - **Question Answering**: Ask questions based on provided context
75
+ - **Image Classification, Detection & Segmentation**
76
+ - **Image Generation**: Generate environmental scenes from prompts
77
+ """)
78
+
79
+ with gr.Tab("🏷 Sentence Classification"):
80
+ gr.Markdown("""
81
+ ### Classify environmental sentences into sentiment
82
+ Enter any environmental sentence, and the tool will tell you if the sentiment is Positive 😊, Negative 😟, or Neutral 😐.
83
+ """)
84
+ txt_in = gr.Textbox(label="Enter text", placeholder="E.g., 'The Amazon rainforest is being deforested'")
85
  txt_out = gr.JSON(label="Classification Result")
86
+ sample_btn = gr.Button("Load Sample Sentence")
87
  txt_in.submit(classify_text, txt_in, txt_out)
88
+ sample_btn.click(lambda: SAMPLE_SENTENCES[0], None, txt_in)
89
 
90
+ with gr.Tab("πŸ” Named Entity Recognition"):
91
+ gr.Markdown("""
92
+ ### Extract named entities
93
+ Enter environmental text, and the model will extract entities like **locations, organizations, species**, etc.
94
+ """)
95
  ner_in = gr.Textbox(label="Enter text")
96
  ner_out = gr.JSON(label="Entities")
97
  ner_in.submit(ner_text, ner_in, ner_out)
98
 
99
+ with gr.Tab("πŸ“ Fill-in-the-Blank"):
100
+ gr.Markdown("""
101
+ ### Complete sentences
102
+ Enter a sentence with `[MASK]` token, and AI will predict possible words to fill.
103
+ """)
104
  mlm_in = gr.Textbox(label="Enter sentence with [MASK]")
105
  mlm_out = gr.JSON(label="Predictions")
106
  mlm_in.submit(fill_blank, mlm_in, mlm_out)
107
 
108
+ with gr.Tab("❓ Question Answering"):
109
+ gr.Markdown("""
110
+ ### Ask questions based on context
111
+ Provide context and ask a question. AI will try to answer from the given text.
112
+ """)
113
  context = gr.Textbox(label="Context")
114
  question = gr.Textbox(label="Question")
115
  qa_out = gr.JSON(label="Answer")
116
  gr.Button("Answer").click(answer_question, [context, question], qa_out)
117
 
118
+ with gr.Tab("πŸ–Ό Image Classification"):
119
+ gr.Markdown("### Upload an environmental image to classify it")
120
  img_in = gr.Image(type="pil")
121
  img_out = gr.JSON(label="Labels")
122
  img_in.upload(classify_image, img_in, img_out)
123
 
124
+ with gr.Tab("πŸ•΅οΈ Object Detection"):
125
+ gr.Markdown("### Detect objects in environmental images")
126
  det_in = gr.Image(type="pil")
127
  det_out = gr.JSON(label="Objects")
128
  det_in.upload(detect_objects, det_in, det_out)
129
 
130
+ with gr.Tab("🧩 Segmentation"):
131
+ gr.Markdown("### Segment environmental images into regions")
132
  seg_in = gr.Image(type="pil")
133
  seg_out = gr.JSON(label="Segments")
134
  seg_in.upload(segment_image, seg_in, seg_out)
135
 
136
+ with gr.Tab("🎨 Image Generation"):
137
+ gr.Markdown("""
138
+ ### Generate environmental scenes from a text prompt
139
+ Describe a scene, e.g., "A lush green forest with tall trees and wildlife".
140
+ """)
141
  gen_in = gr.Textbox(label="Prompt")
142
  gen_out = gr.Image(label="Generated Image")
143
  gr.Button("Generate").click(generate_image, gen_in, gen_out)
144
 
 
145
  demo.launch()