Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,10 @@ from gtts import gTTS
|
|
| 9 |
import speech_recognition as sr
|
| 10 |
import os
|
| 11 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# from langdetect import detect
|
| 13 |
|
| 14 |
|
|
@@ -56,6 +60,7 @@ def transcribe_audio(audio_path):
|
|
| 56 |
except sr.RequestError:
|
| 57 |
return "❌ ਗੂਗਲ ਸਪੀਚ ਐਪੀਆਈ ਨਾਲ ਕਨੇਕਟ ਨਹੀਂ ਹੋ ਸਕਿਆ।"
|
| 58 |
|
|
|
|
| 59 |
# ---------------------------
|
| 60 |
# Gemini Response & TTS
|
| 61 |
# ---------------------------
|
|
@@ -80,7 +85,47 @@ def handle_voice_query(audio_file):
|
|
| 80 |
response = get_gemini_response(query)
|
| 81 |
audio_path = text_to_speech(response)
|
| 82 |
return query, response, audio_path
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# ---------------------------
|
| 85 |
# Gradio Interface
|
| 86 |
# ---------------------------
|
|
@@ -111,5 +156,11 @@ with gr.Blocks() as demo:
|
|
| 111 |
audio_output = gr.Audio(label="🔊 ਆਵਾਜ਼ੀ ਜਵਾਬ")
|
| 112 |
submit_btn = gr.Button("➡️ ਜਵਾਬ ਲਵੋ")
|
| 113 |
submit_btn.click(fn=handle_voice_query, inputs=[audio_input], outputs=[query_text, gemini_response, audio_output])
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
demo.launch()
|
|
|
|
| 9 |
import speech_recognition as sr
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
+
import torch
|
| 13 |
+
from torchvision import models, transforms
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import json
|
| 16 |
# from langdetect import detect
|
| 17 |
|
| 18 |
|
|
|
|
| 60 |
except sr.RequestError:
|
| 61 |
return "❌ ਗੂਗਲ ਸਪੀਚ ਐਪੀਆਈ ਨਾਲ ਕਨੇਕਟ ਨਹੀਂ ਹੋ ਸਕਿਆ।"
|
| 62 |
|
| 63 |
+
|
| 64 |
# ---------------------------
|
| 65 |
# Gemini Response & TTS
|
| 66 |
# ---------------------------
|
|
|
|
| 85 |
response = get_gemini_response(query)
|
| 86 |
audio_path = text_to_speech(response)
|
| 87 |
return query, response, audio_path
|
| 88 |
+
# ---------------------------
|
| 89 |
+
# Plant Disease Detection
|
| 90 |
+
# ---------------------------
|
| 91 |
+
# Set device
|
| 92 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 93 |
+
# Paths to files
|
| 94 |
+
model_path = "mobilenetv3_plant_disease.pth"
|
| 95 |
+
class_names_path = "class_labels.json"
|
| 96 |
+
|
| 97 |
+
# Load model
|
| 98 |
+
model_disease = models.mobilenet_v3_small(pretrained=False)
|
| 99 |
+
model_disease.classifier[3] = torch.nn.Linear(model_disease.classifier[3].in_features, 38)
|
| 100 |
+
model_disease.load_state_dict(torch.load(model_path, map_location=device))
|
| 101 |
+
model_disease.to(device)
|
| 102 |
+
model_disease.eval()
|
| 103 |
+
|
| 104 |
+
# Load and reverse class names
|
| 105 |
+
with open(class_names_path, 'r') as f:
|
| 106 |
+
label_to_index = json.load(f)
|
| 107 |
+
index_to_label = {v: k for k, v in label_to_index.items()}
|
| 108 |
+
|
| 109 |
+
# Define transform
|
| 110 |
+
transform = transforms.Compose([
|
| 111 |
+
transforms.Resize((224, 224)),
|
| 112 |
+
transforms.ToTensor()
|
| 113 |
+
])
|
| 114 |
+
|
| 115 |
+
# Prediction function
|
| 116 |
+
def predict_disease(image_path):
|
| 117 |
+
try:
|
| 118 |
+
image = Image.open(image_path).convert("RGB")
|
| 119 |
+
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 120 |
+
with torch.no_grad():
|
| 121 |
+
outputs = model_disease(img_tensor)
|
| 122 |
+
_, predicted = torch.max(outputs, 1)
|
| 123 |
+
predicted_class = predicted.item()
|
| 124 |
+
class_name = index_to_label.get(predicted_class, "Unknown class")
|
| 125 |
+
return f"🌿 Predicted Disease: *{class_name.replace('_', ' ')}*"
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"❌ Prediction Failed: {str(e)}"
|
| 128 |
+
|
| 129 |
# ---------------------------
|
| 130 |
# Gradio Interface
|
| 131 |
# ---------------------------
|
|
|
|
| 156 |
audio_output = gr.Audio(label="🔊 ਆਵਾਜ਼ੀ ਜਵਾਬ")
|
| 157 |
submit_btn = gr.Button("➡️ ਜਵਾਬ ਲਵੋ")
|
| 158 |
submit_btn.click(fn=handle_voice_query, inputs=[audio_input], outputs=[query_text, gemini_response, audio_output])
|
| 159 |
+
|
| 160 |
+
with gr.TabItem("Plant Disease Detection"):
|
| 161 |
+
gr.Markdown("### Upload a crop leaf image to detect disease")
|
| 162 |
+
image_input = gr.Image(type="filepath", label="📷 Upload Leaf Image")
|
| 163 |
+
disease_btn = gr.Button("Detect Disease")
|
| 164 |
+
disease_output = gr.Markdown()
|
| 165 |
+
disease_btn.click(fn=predict_disease, inputs=image_input, outputs=disease_output)
|
| 166 |
demo.launch()
|