Spaces:
Sleeping
Sleeping
main
#2
by Amithkuppili - opened
- app.py +59 -45
- requirements.txt +8 -3
- resnet18_cifar10.pt +3 -0
- resnet18_quantized.pt +3 -0
app.py
CHANGED
|
@@ -1,47 +1,41 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
from PIL import Image
|
| 3 |
-
import gradio as gr
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
text_classifier = pipeline(
|
| 9 |
-
"text-classification",
|
| 10 |
-
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 11 |
-
)
|
| 12 |
-
# Fake news detection
|
| 13 |
-
fake_detector = pipeline(
|
| 14 |
-
"text-classification",
|
| 15 |
-
model="mrm8488/bert-tiny-finetuned-fake-news-detection" # public
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
image_classifier = pipeline(
|
| 19 |
-
"image-classification",
|
| 20 |
-
model="microsoft/resnet-50" # public
|
| 21 |
-
)
|
| 22 |
|
|
|
|
| 23 |
leaf_classifier = pipeline(
|
| 24 |
"image-classification",
|
| 25 |
-
model="
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
)
|
| 32 |
|
| 33 |
|
|
|
|
| 34 |
def classify_text(text):
|
| 35 |
result = text_classifier(text)[0]
|
| 36 |
return {result['label']: float(result['score'])}
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def detect_fake(text):
|
| 39 |
result = fake_detector(text)[0]
|
| 40 |
return {result['label']: float(result['score'])}
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
def detect_leaf_disease(image):
|
| 47 |
result = leaf_classifier(image)[0]
|
|
@@ -51,27 +45,47 @@ def detect_fruitveg(image):
|
|
| 51 |
result = fruitveg_classifier(image)[0]
|
| 52 |
return {result['label']: float(result['score'])}
|
| 53 |
|
|
|
|
|
|
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
with gr.Tab("
|
| 58 |
-
gr.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
with gr.Tab("
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
def classify_and_detect(text):
|
| 73 |
-
t_result = classify_text(text)
|
| 74 |
-
f_result = detect_fake(text)
|
| 75 |
-
return t_result, f_result
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
+
# ---- Load models ----
|
| 6 |
+
text_classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 7 |
|
| 8 |
+
image_classifier = pipeline("image-classification", model="microsoft/resnet-50")
|
| 9 |
|
| 10 |
+
# audio_classifier = pipeline("audio-classification", model="superb/wav2vec2-base-superb-ks")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Leaf disease detection (public model)
|
| 13 |
leaf_classifier = pipeline(
|
| 14 |
"image-classification",
|
| 15 |
+
model="nateraw/plant-disease-classification"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
|
| 19 |
+
fruitveg_classifier = pipeline("image-classification", model="mrdbourke/mobilenet_v2_fruit")
|
| 20 |
+
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
+
# ---- Define functions ----
|
| 24 |
def classify_text(text):
|
| 25 |
result = text_classifier(text)[0]
|
| 26 |
return {result['label']: float(result['score'])}
|
| 27 |
|
| 28 |
+
def classify_image(image):
|
| 29 |
+
result = image_classifier(image)[0]
|
| 30 |
+
return {result['label']: float(result['score'])}
|
| 31 |
+
|
| 32 |
def detect_fake(text):
|
| 33 |
result = fake_detector(text)[0]
|
| 34 |
return {result['label']: float(result['score'])}
|
| 35 |
|
| 36 |
+
# def classify_audio(audio):
|
| 37 |
+
# result = audio_classifier(audio)[0]
|
| 38 |
+
# return {result['label']: float(result['score'])}
|
| 39 |
|
| 40 |
def detect_leaf_disease(image):
|
| 41 |
result = leaf_classifier(image)[0]
|
|
|
|
| 45 |
result = fruitveg_classifier(image)[0]
|
| 46 |
return {result['label']: float(result['score'])}
|
| 47 |
|
| 48 |
+
|
| 49 |
+
# ---- Build Gradio interface ----
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
+
gr.Markdown("# 🧩 Multi-Model v1.0\nSupports **Text & Image Classification**")
|
| 52 |
+
|
| 53 |
+
with gr.Tab("Text Classifier"):
|
| 54 |
+
txt_input = gr.Textbox(label="Enter text")
|
| 55 |
+
txt_output = gr.Label()
|
| 56 |
+
txt_button = gr.Button("Classify Text")
|
| 57 |
+
txt_button.click(fn=classify_text, inputs=txt_input, outputs=txt_output)
|
| 58 |
+
|
| 59 |
+
with gr.Tab("Image Classifier"):
|
| 60 |
+
img_input = gr.Image(type="pil", label="Upload image")
|
| 61 |
+
img_output = gr.Label()
|
| 62 |
+
img_button = gr.Button("Classify Image")
|
| 63 |
+
img_button.click(fn=classify_image, inputs=img_input, outputs=img_output)
|
| 64 |
+
|
| 65 |
+
# with gr.Tab("Audio Classifier"):
|
| 66 |
+
# aud_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio")
|
| 67 |
+
# aud_output = gr.Label()
|
| 68 |
+
# aud_button = gr.Button("Classify Aut, outputs=aud_dio")
|
| 69 |
+
# aud_button.click(fn=classify_audio, inputs=aud_inpuoutput)
|
| 70 |
+
|
| 71 |
+
with gr.Tab("Fake News Detector"):
|
| 72 |
+
fake_input = gr.Textbox(label="Enter news/article text")
|
| 73 |
+
fake_output = gr.Label()
|
| 74 |
+
fake_button = gr.Button("Detect Fake")
|
| 75 |
+
fake_button.click(fn=detect_fake, inputs=fake_input, outputs=fake_output)
|
| 76 |
+
|
| 77 |
+
with gr.Tab("Leaf Disease Detector"):
|
| 78 |
+
leaf_input = gr.Image(type="pil", label="Upload leaf image")
|
| 79 |
+
leaf_output = gr.Label()
|
| 80 |
+
leaf_button = gr.Button("Detect Disease")
|
| 81 |
+
leaf_button.click(fn=detect_leaf_disease, inputs=leaf_input, outputs=leaf_output)
|
| 82 |
+
|
| 83 |
+
with gr.Tab("Fruit & Veg Detector"):
|
| 84 |
+
fv_input = gr.Image(type="pil", label="Upload fruit/vegetable image")
|
| 85 |
+
fv_output = gr.Label()
|
| 86 |
+
fv_button = gr.Button("Detect")
|
| 87 |
+
fv_button.click(fn=detect_fruitveg, inputs=fv_input, outputs=fv_output)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
|
| 91 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
transformers
|
| 3 |
torch
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
soundfile
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
fastapi
|
| 5 |
+
uvicorn[standard]
|
| 6 |
+
aiofiles
|
| 7 |
+
python-multipart
|
| 8 |
+
pillow
|
| 9 |
soundfile
|
| 10 |
+
gradio
|
resnet18_cifar10.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6829b35ffa842ed3eaa733182d8226ece82f77c052b75c802242318d735f9896
|
| 3 |
+
size 11402991
|
resnet18_quantized.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:54868efdd06d073148f5f06a49bff601bded49386a4b83d312001ca71338193a
|
| 3 |
+
size 11403367
|