navjotk commited on
Commit
089b37c
Β·
verified Β·
1 Parent(s): fb9206d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -5,14 +5,16 @@ 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
 
@@ -38,26 +40,15 @@ def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
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:
63
  response = gemini_model.generate_content(prompt)
@@ -65,12 +56,11 @@ def chat_with_gemini(prompt):
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
@@ -100,7 +90,7 @@ with gr.Blocks() as demo:
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")
@@ -109,21 +99,19 @@ with gr.Blocks() as demo:
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()
 
5
  import os
6
  from sklearn.model_selection import train_test_split
7
  from sklearn.preprocessing import LabelEncoder
8
+ from transformers import pipeline, set_seed
 
9
  import google.generativeai as genai
10
 
11
  # ---------------------------
12
+ # Load environment variables
13
  # ---------------------------
14
+ GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE"
15
+ # Add your actual API key here
16
+
17
+ # Configure Gemini API
18
  genai.configure(api_key=GOOGLE_API_KEY)
19
  gemini_model = genai.GenerativeModel("models/gemini-1.5-pro")
20
 
 
40
  return f"🌾 Recommended Crop: *{crop_name}*"
41
 
42
  # ---------------------------
43
+ # CHATBOT SETUP
44
  # ---------------------------
45
+ chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
46
+ set_seed(42)
47
+
48
+ def chat_with_bot(user_input):
49
+ conversation = chatbot(user_input)
50
+ return conversation[0]['generated_text']
 
 
 
 
 
 
 
 
51
 
 
 
 
52
  def chat_with_gemini(prompt):
53
  try:
54
  response = gemini_model.generate_content(prompt)
 
56
  except Exception as e:
57
  return f"❌ Error from Gemini: {str(e)}"
58
 
59
+ def smart_chat(user_input, model_choice):
60
+ if model_choice == "Gemini":
61
+ return chat_with_gemini(user_input)
62
+ else:
63
+ return chat_with_bot(user_input)
 
64
 
65
  # ---------------------------
66
  # GRADIO APP UI
 
90
  outputs=output_crop
91
  )
92
 
93
+ # TAB 2: CROP DISEASE PREDICTION (Placeholder)
94
  with gr.TabItem("🌿 Crop Disease Detection"):
95
  gr.Markdown("### Upload an image of a crop leaf to detect disease (Coming Soon)")
96
  gr.Image(label="Upload Crop Image", type="filepath")
 
99
 
100
  # TAB 3: SMART CHATBOT
101
  with gr.TabItem("πŸ’¬ Farmer's Chatbot"):
102
+ gr.Markdown("### Ask any question related to farming πŸ‘¨β€πŸŒΎ")
103
 
104
  with gr.Row():
105
+ user_input = gr.Textbox(label="Your Question")
106
+ model_selector = gr.Dropdown(["Gemini", "DialoGPT"], value="Gemini", label="Select Model")
107
+
108
+ # Audio input from microphone
109
+ audio_input = gr.Audio(type="filepath", label="🎀 Speak Your Question")
110
 
 
111
  chatbot_output = gr.Textbox(label="AgroVision Bot Response")
112
  chatbot_btn = gr.Button("Ask")
113
 
114
+ chatbot_btn.click(fn=smart_chat, inputs=[user_input, model_selector], outputs=chatbot_output)
 
 
 
 
115
 
116
  # Launch the app
117
  demo.launch()