Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,111 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from gtts import gTTS
|
| 3 |
-
import os
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
import speech_recognition as sr
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return response.text
|
| 18 |
except Exception as e:
|
| 19 |
-
return f"❌
|
| 20 |
|
| 21 |
-
|
| 22 |
-
def text_to_speech(text, lang="pa"):
|
| 23 |
tts = gTTS(text=text, lang=lang)
|
| 24 |
-
|
| 25 |
-
tts.save(
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
# Function to handle voice input and output
|
| 29 |
-
def respond_with_audio_and_text(audio_file):
|
| 30 |
-
try:
|
| 31 |
-
# Convert WebM to WAV
|
| 32 |
-
sound = AudioSegment.from_file(audio_file, format="webm")
|
| 33 |
-
wav_path = "converted.wav"
|
| 34 |
-
sound.export(wav_path, format="wav")
|
| 35 |
-
|
| 36 |
-
# Use speech recognition
|
| 37 |
-
recognizer = sr.Recognizer()
|
| 38 |
-
with sr.AudioFile(wav_path) as source:
|
| 39 |
-
audio = recognizer.record(source)
|
| 40 |
-
user_text = recognizer.recognize_google(audio, language="pa-IN")
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return f"❌ Voice Input Error: {str(e)}", None
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
-
gr.Markdown("
|
| 51 |
-
gr.Markdown("🎤 Speak in Punjabi and get a smart Gemini-based response!")
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import lightgbm as lgb
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
from google.generativeai import GenerativeModel, configure
|
| 8 |
from gtts import gTTS
|
|
|
|
|
|
|
| 9 |
import speech_recognition as sr
|
| 10 |
+
import os
|
| 11 |
+
import tempfile
|
| 12 |
+
|
| 13 |
+
# ---------------------------
|
| 14 |
+
# Gemini Configuration
|
| 15 |
+
# ---------------------------
|
| 16 |
+
GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE" # Replace with your API key in double quotes
|
| 17 |
+
configure(api_key=GOOGLE_API_KEY)
|
| 18 |
+
gemini_model = GenerativeModel("models/gemini-1.5-pro")
|
| 19 |
+
|
| 20 |
+
# ---------------------------
|
| 21 |
+
# Crop Recommendation Setup
|
| 22 |
+
# ---------------------------
|
| 23 |
+
url = "https://raw.githubusercontent.com/89911384/CSV-Files/refs/heads/main/crop_cleaned%20data.csv"
|
| 24 |
+
data = pd.read_csv(url)
|
| 25 |
+
|
| 26 |
+
X = data.drop('label', axis=1)
|
| 27 |
+
y = data['label']
|
| 28 |
+
le = LabelEncoder()
|
| 29 |
+
y_encoded = le.fit_transform(y)
|
| 30 |
+
|
| 31 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.3, random_state=0)
|
| 32 |
+
model = lgb.LGBMClassifier()
|
| 33 |
+
model.fit(X_train, y_train)
|
| 34 |
|
| 35 |
+
def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
| 36 |
+
input_data = np.array([[N, P, K, temperature, humidity, ph, rainfall]])
|
| 37 |
+
pred = model.predict(input_data)[0]
|
| 38 |
+
crop_name = le.inverse_transform([pred])[0]
|
| 39 |
+
return f"🌾 ਸਿਫਾਰਸ਼ੀ ਫਸਲ: *{crop_name}*"
|
| 40 |
|
| 41 |
+
# ---------------------------
|
| 42 |
+
# Voice to Text Utility
|
| 43 |
+
# ---------------------------
|
| 44 |
+
def transcribe_audio(audio_path):
|
| 45 |
+
recognizer = sr.Recognizer()
|
| 46 |
+
with sr.AudioFile(audio_path) as source:
|
| 47 |
+
audio = recognizer.record(source)
|
| 48 |
try:
|
| 49 |
+
return recognizer.recognize_google(audio, language='pa-IN')
|
| 50 |
+
except sr.UnknownValueError:
|
| 51 |
+
return "❌ ਆਵਾਜ਼ ਨੂੰ ਸਮਝਿਆ ਨਹੀਂ ਜਾ ਸਕਿਆ।"
|
| 52 |
+
except sr.RequestError:
|
| 53 |
+
return "❌ ਗੂਗਲ ਸਪੀਚ ਐਪੀਆਈ ਨਾਲ ਕਨੇਕਟ ਨਹੀਂ ਹੋ ਸਕਿਆ।"
|
| 54 |
+
|
| 55 |
+
# ---------------------------
|
| 56 |
+
# Gemini Response & TTS
|
| 57 |
+
# ---------------------------
|
| 58 |
+
def get_gemini_response(query):
|
| 59 |
+
try:
|
| 60 |
+
response = gemini_model.generate_content(f"ਪੰਜਾਬੀ ਵਿੱਚ ਜਵਾਬ ਦਿਓ: {query}")
|
| 61 |
return response.text
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"❌ Gemini ਤਰਫੋਂ ਗਲਤੀ: {str(e)}"
|
| 64 |
|
| 65 |
+
def text_to_speech(text, lang='pa'):
|
|
|
|
| 66 |
tts = gTTS(text=text, lang=lang)
|
| 67 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 68 |
+
tts.save(temp_file.name)
|
| 69 |
+
return temp_file.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# ---------------------------
|
| 72 |
+
# Combined Function
|
| 73 |
+
# ---------------------------
|
| 74 |
+
def handle_voice_query(audio_file):
|
| 75 |
+
query = transcribe_audio(audio_file)
|
| 76 |
+
response = get_gemini_response(query)
|
| 77 |
+
audio_path = text_to_speech(response)
|
| 78 |
+
return query, response, audio_path
|
| 79 |
|
| 80 |
+
# ---------------------------
|
| 81 |
+
# Gradio Interface
|
| 82 |
+
# ---------------------------
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown("# 🌾 **AgroVision: ਪੰਜਾਬੀ ਵੈਚਲਣ ਸਹਾਇਕ**")
|
|
|
|
| 85 |
|
| 86 |
+
with gr.Tabs():
|
| 87 |
+
with gr.TabItem("🌾 ਫਸਲ ਦੀ ਸਿਫਾਰਸ਼"):
|
| 88 |
+
with gr.Row():
|
| 89 |
+
N = gr.Slider(0, 300, step=1, label="ਨਾਈਟ੍ਰੋਜਨ (kg/ha)")
|
| 90 |
+
P = gr.Slider(0, 200, step=1, label="ਫਾਸਫੋਰਸ (kg/ha)")
|
| 91 |
+
K = gr.Slider(0, 200, step=1, label="ਪੋਟਾਸ਼ੀਅਮ (kg/ha)")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
temperature = gr.Slider(-10, 50, step=0.1, label="ਤਾਪਮਾਨ (°C)")
|
| 94 |
+
humidity = gr.Slider(0, 100, step=1, label="ਨਮੀ (%)")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
ph = gr.Slider(0, 14, step=0.1, label="ਮਿੱਟੀ ਦਾ pH")
|
| 97 |
+
rainfall = gr.Slider(0, 500, step=1, label="ਵਰਖਾ (mm)")
|
| 98 |
+
predict_btn = gr.Button("ਫਸਲ ਦੀ ਭਵਿੱਖਬਾਣੀ ਕਰੋ")
|
| 99 |
+
crop_output = gr.Markdown()
|
| 100 |
+
predict_btn.click(predict_crop, inputs=[N, P, K, temperature, humidity, ph, rainfall], outputs=crop_output)
|
| 101 |
|
| 102 |
+
with gr.TabItem("🗣️ ਆਵਾਜ਼ ਰਾਹੀਂ ਪੁੱਛੋ"):
|
| 103 |
+
gr.Markdown("### ਆਪਣਾ ਸਵਾਲ ਆਵਾਜ਼ ਰਾਹੀਂ ਪੁੱਛੋ (ਪੰਜਾਬੀ ਵਿੱਚ)")
|
| 104 |
+
audio_input = gr.Audio(type="filepath", label="🎤 ਸਵਾਲ ਬੋਲੋ")
|
| 105 |
+
query_text = gr.Textbox(label="🔍 ਬੋਲਿਆ ਗਿਆ ਸਵਾਲ")
|
| 106 |
+
gemini_response = gr.Textbox(label="📜 Gemini ਜਵਾਬ")
|
| 107 |
+
audio_output = gr.Audio(label="🔊 ਆਵਾਜ਼ੀ ਜਵਾਬ")
|
| 108 |
+
submit_btn = gr.Button("➡️ ਜਵਾਬ ਲਵੋ")
|
| 109 |
+
submit_btn.click(fn=handle_voice_query, inputs=[audio_input], outputs=[query_text, gemini_response, audio_output])
|
| 110 |
|
| 111 |
demo.launch()
|