Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,19 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import lightgbm as lgb
|
| 4 |
import numpy as np
|
| 5 |
-
import
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ---------------------------
|
| 10 |
-
#
|
| 11 |
# ---------------------------
|
| 12 |
-
GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE" #
|
| 13 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 14 |
-
|
| 15 |
-
# Load Gemini model
|
| 16 |
-
gemini_model = genai.GenerativeModel(model_name="models/gemini-1.5-pro")
|
| 17 |
|
| 18 |
# ---------------------------
|
| 19 |
# CROP RECOMMENDATION SETUP
|
|
@@ -37,7 +38,25 @@ def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
|
| 37 |
return f"πΎ Recommended Crop: *{crop_name}*"
|
| 38 |
|
| 39 |
# ---------------------------
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# ---------------------------
|
| 42 |
def chat_with_gemini(prompt):
|
| 43 |
try:
|
|
@@ -46,6 +65,13 @@ def chat_with_gemini(prompt):
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"β Error from Gemini: {str(e)}"
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# ---------------------------
|
| 50 |
# GRADIO APP UI
|
| 51 |
# ---------------------------
|
|
@@ -74,20 +100,30 @@ with gr.Blocks() as demo:
|
|
| 74 |
outputs=output_crop
|
| 75 |
)
|
| 76 |
|
| 77 |
-
# TAB 2: CROP DISEASE
|
| 78 |
with gr.TabItem("πΏ Crop Disease Detection"):
|
| 79 |
gr.Markdown("### Upload an image of a crop leaf to detect disease (Coming Soon)")
|
| 80 |
gr.Image(label="Upload Crop Image", type="filepath")
|
| 81 |
gr.Button("Predict Disease (Coming Soon)")
|
| 82 |
gr.Textbox(label="Prediction Output", placeholder="Model response will appear here...")
|
| 83 |
|
| 84 |
-
# TAB 3: SMART CHATBOT
|
| 85 |
with gr.TabItem("π¬ Farmer's Chatbot"):
|
| 86 |
-
gr.Markdown("### Ask
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
chatbot_output = gr.Textbox(label="AgroVision Bot Response")
|
| 89 |
-
chatbot_btn = gr.Button("Ask
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
demo.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import lightgbm as lgb
|
| 4 |
import numpy as np
|
| 5 |
+
import os
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
import speech_recognition as sr
|
| 9 |
+
from pydub import AudioSegment
|
| 10 |
+
import google.generativeai as genai
|
| 11 |
|
| 12 |
# ---------------------------
|
| 13 |
+
# CONFIGURE GEMINI API
|
| 14 |
# ---------------------------
|
| 15 |
+
GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE" # π Keep this secure
|
| 16 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 17 |
+
gemini_model = genai.GenerativeModel("models/gemini-1.5-pro")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ---------------------------
|
| 20 |
# CROP RECOMMENDATION SETUP
|
|
|
|
| 38 |
return f"πΎ Recommended Crop: *{crop_name}*"
|
| 39 |
|
| 40 |
# ---------------------------
|
| 41 |
+
# VOICE TO TEXT FUNCTION
|
| 42 |
+
# ---------------------------
|
| 43 |
+
def voice_to_text(audio_path):
|
| 44 |
+
recognizer = sr.Recognizer()
|
| 45 |
+
audio = AudioSegment.from_file(audio_path)
|
| 46 |
+
audio.export("converted.wav", format="wav")
|
| 47 |
+
|
| 48 |
+
with sr.AudioFile("converted.wav") as source:
|
| 49 |
+
audio_data = recognizer.record(source)
|
| 50 |
+
try:
|
| 51 |
+
text = recognizer.recognize_google(audio_data, language="pa-IN") # Punjabi
|
| 52 |
+
return text
|
| 53 |
+
except sr.UnknownValueError:
|
| 54 |
+
return "β Unable to understand the audio."
|
| 55 |
+
except sr.RequestError as e:
|
| 56 |
+
return f"β Request Error: {e}"
|
| 57 |
+
|
| 58 |
+
# ---------------------------
|
| 59 |
+
# GEMINI CHATBOT FUNCTION
|
| 60 |
# ---------------------------
|
| 61 |
def chat_with_gemini(prompt):
|
| 62 |
try:
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"β Error from Gemini: {str(e)}"
|
| 67 |
|
| 68 |
+
def handle_query(text_input, audio_file, model_choice):
|
| 69 |
+
if not text_input and audio_file:
|
| 70 |
+
text_input = voice_to_text(audio_file)
|
| 71 |
+
if not text_input:
|
| 72 |
+
return "β Please speak or type your question."
|
| 73 |
+
return chat_with_gemini(text_input)
|
| 74 |
+
|
| 75 |
# ---------------------------
|
| 76 |
# GRADIO APP UI
|
| 77 |
# ---------------------------
|
|
|
|
| 100 |
outputs=output_crop
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# TAB 2: CROP DISEASE (Placeholder)
|
| 104 |
with gr.TabItem("πΏ Crop Disease Detection"):
|
| 105 |
gr.Markdown("### Upload an image of a crop leaf to detect disease (Coming Soon)")
|
| 106 |
gr.Image(label="Upload Crop Image", type="filepath")
|
| 107 |
gr.Button("Predict Disease (Coming Soon)")
|
| 108 |
gr.Textbox(label="Prediction Output", placeholder="Model response will appear here...")
|
| 109 |
|
| 110 |
+
# TAB 3: SMART CHATBOT
|
| 111 |
with gr.TabItem("π¬ Farmer's Chatbot"):
|
| 112 |
+
gr.Markdown("### Ask your question in Punjabi by text or voice ποΈ")
|
| 113 |
+
|
| 114 |
+
with gr.Row():
|
| 115 |
+
user_input = gr.Textbox(label="Type Your Question")
|
| 116 |
+
audio_input = gr.Audio(source="microphone", type="filepath", label="π€ Speak Your Question")
|
| 117 |
+
|
| 118 |
+
model_selector = gr.Dropdown(["Gemini"], value="Gemini", label="Select Model (Only Gemini Supported)")
|
| 119 |
chatbot_output = gr.Textbox(label="AgroVision Bot Response")
|
| 120 |
+
chatbot_btn = gr.Button("Ask")
|
| 121 |
+
|
| 122 |
+
chatbot_btn.click(
|
| 123 |
+
fn=handle_query,
|
| 124 |
+
inputs=[user_input, audio_input, model_selector],
|
| 125 |
+
outputs=chatbot_output
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
# Launch the app
|
| 129 |
demo.launch()
|