arjunvankani commited on
Commit
0236db7
·
verified ·
1 Parent(s): dd39af7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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")
8
+ ner = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
9
+ mlm = pipeline("fill-mask", model="bert-base-uncased")
10
+ qa = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
11
+
12
+ # --- Vision pipelines ---
13
+ img_clf = pipeline("image-classification", model="google/vit-base-patch16-224")
14
+ det = pipeline("object-detection", model="facebook/detr-resnet-50")
15
+ seg = pipeline("image-segmentation", model="facebook/mask2former-swin-large-coco")
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
+ # --- Speech ---
22
+ asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
23
+ tts = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
24
+
25
+ # --- Functions ---
26
+ def classify_text(text):
27
+ return clf(text)
28
+
29
+ def ner_text(text):
30
+ return ner(text)
31
+
32
+ def fill_blank(text):
33
+ return mlm(text)
34
+
35
+ def answer_question(context, question):
36
+ return qa(question=question, context=context)
37
+
38
+ def classify_image(image):
39
+ return img_clf(image)
40
+
41
+ def detect_objects(image):
42
+ return det(image)
43
+
44
+ def segment_image(image):
45
+ return seg(image)
46
+
47
+ def generate_image(prompt):
48
+ image = sd_pipe(prompt).images[0]
49
+ return image
50
+
51
+ def transcribe(audio):
52
+ return asr(audio)["text"]
53
+
54
+ def speak_text(text):
55
+ audio = tts(text)
56
+ return (audio["sample_rate"], audio["audio"])
57
+
58
+ # --- Gradio Interface ---
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("# 🌍 Environmental AI Toolkit")
61
+
62
+ with gr.Tab("Sentence Classification"):
63
+ txt_in = gr.Textbox(label="Enter text")
64
+ txt_out = gr.JSON(label="Classification Result")
65
+ txt_in.submit(classify_text, txt_in, txt_out)
66
+
67
+ with gr.Tab("NER"):
68
+ ner_in = gr.Textbox(label="Enter text")
69
+ ner_out = gr.JSON(label="Entities")
70
+ ner_in.submit(ner_text, ner_in, ner_out)
71
+
72
+ with gr.Tab("Fill-in-the-Blank"):
73
+ mlm_in = gr.Textbox(label="Enter sentence with [MASK]")
74
+ mlm_out = gr.JSON(label="Predictions")
75
+ mlm_in.submit(fill_blank, mlm_in, mlm_out)
76
+
77
+ with gr.Tab("Question Answering"):
78
+ context = gr.Textbox(label="Context")
79
+ question = gr.Textbox(label="Question")
80
+ qa_out = gr.JSON(label="Answer")
81
+ gr.Button("Answer").click(answer_question, [context, question], qa_out)
82
+
83
+ with gr.Tab("Image Classification"):
84
+ img_in = gr.Image(type="pil")
85
+ img_out = gr.JSON(label="Labels")
86
+ img_in.upload(classify_image, img_in, img_out)
87
+
88
+ with gr.Tab("Object Detection"):
89
+ det_in = gr.Image(type="pil")
90
+ det_out = gr.JSON(label="Objects")
91
+ det_in.upload(detect_objects, det_in, det_out)
92
+
93
+ with gr.Tab("Segmentation"):
94
+ seg_in = gr.Image(type="pil")
95
+ seg_out = gr.JSON(label="Segments")
96
+ seg_in.upload(segment_image, seg_in, seg_out)
97
+
98
+ with gr.Tab("Image Generation"):
99
+ gen_in = gr.Textbox(label="Prompt")
100
+ gen_out = gr.Image(label="Generated Image")
101
+ gr.Button("Generate").click(generate_image, gen_in, gen_out)
102
+
103
+ with gr.Tab("Speech Recognition"):
104
+ audio_in = gr.Audio(type="filepath")
105
+ audio_out = gr.Textbox(label="Transcription")
106
+ audio_in.change(transcribe, audio_in, audio_out)
107
+
108
+ with gr.Tab("Text-to-Speech"):
109
+ tts_in = gr.Textbox(label="Text to Speak")
110
+ tts_out = gr.Audio(label="Generated Speech")
111
+ gr.Button("Speak").click(speak_text, tts_in, tts_out)
112
+
113
+ demo.launch()