Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,18 @@ 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 |
-
from transformers import pipeline, Conversation, set_seed
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
import google.generativeai as genai
|
| 11 |
|
| 12 |
# ---------------------------
|
| 13 |
-
#
|
| 14 |
# ---------------------------
|
| 15 |
-
#
|
| 16 |
-
GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE"
|
| 17 |
-
|
| 18 |
-
# Configure Gemini API
|
| 19 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# ---------------------------
|
| 23 |
# CROP RECOMMENDATION SETUP
|
|
@@ -41,16 +37,8 @@ def predict_crop(N, P, K, temperature, humidity, ph, rainfall):
|
|
| 41 |
return f"🌾 Recommended Crop: *{crop_name}*"
|
| 42 |
|
| 43 |
# ---------------------------
|
| 44 |
-
# CHATBOT
|
| 45 |
# ---------------------------
|
| 46 |
-
chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 47 |
-
set_seed(42)
|
| 48 |
-
|
| 49 |
-
def chat_with_bot(user_input):
|
| 50 |
-
conversation = Conversation(user_input)
|
| 51 |
-
conversation = chatbot(conversation)
|
| 52 |
-
return conversation.generated_responses[-1]
|
| 53 |
-
|
| 54 |
def chat_with_gemini(prompt):
|
| 55 |
try:
|
| 56 |
response = gemini_model.generate_content(prompt)
|
|
@@ -58,12 +46,6 @@ def chat_with_gemini(prompt):
|
|
| 58 |
except Exception as e:
|
| 59 |
return f"❌ Error from Gemini: {str(e)}"
|
| 60 |
|
| 61 |
-
def smart_chat(user_input, model_choice):
|
| 62 |
-
if model_choice == "Gemini":
|
| 63 |
-
return chat_with_gemini(user_input)
|
| 64 |
-
else:
|
| 65 |
-
return chat_with_bot(user_input)
|
| 66 |
-
|
| 67 |
# ---------------------------
|
| 68 |
# GRADIO APP UI
|
| 69 |
# ---------------------------
|
|
@@ -99,16 +81,13 @@ with gr.Blocks() as demo:
|
|
| 99 |
gr.Button("Predict Disease (Coming Soon)")
|
| 100 |
gr.Textbox(label="Prediction Output", placeholder="Model response will appear here...")
|
| 101 |
|
| 102 |
-
# TAB 3: SMART CHATBOT
|
| 103 |
with gr.TabItem("💬 Farmer's Chatbot"):
|
| 104 |
gr.Markdown("### Ask any question related to farming 👨🌾")
|
| 105 |
-
|
| 106 |
-
with gr.Row():
|
| 107 |
-
user_input = gr.Textbox(label="Your Question")
|
| 108 |
-
model_selector = gr.Dropdown(["Gemini", "DialoGPT"], value="Gemini", label="Select Model")
|
| 109 |
chatbot_output = gr.Textbox(label="AgroVision Bot Response")
|
| 110 |
-
chatbot_btn = gr.Button("Ask")
|
| 111 |
-
chatbot_btn.click(fn=
|
| 112 |
|
| 113 |
-
#
|
| 114 |
demo.launch()
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import lightgbm as lgb
|
| 4 |
import numpy as np
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ---------------------------
|
| 10 |
+
# HARDCODED GEMINI API KEY
|
| 11 |
# ---------------------------
|
| 12 |
+
GOOGLE_API_KEY = "AIzaSyAju28ijKpMNxr1kh4Ml5GPNmI7reBN7FE" # Replace with your actual Gemini API key
|
|
|
|
|
|
|
|
|
|
| 13 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 14 |
+
|
| 15 |
+
# Load Gemini model
|
| 16 |
+
gemini_model = genai.GenerativeModel(model_name="models/gemini-pro")
|
| 17 |
|
| 18 |
# ---------------------------
|
| 19 |
# CROP RECOMMENDATION SETUP
|
|
|
|
| 37 |
return f"🌾 Recommended Crop: *{crop_name}*"
|
| 38 |
|
| 39 |
# ---------------------------
|
| 40 |
+
# CHATBOT FUNCTION (GEMINI ONLY)
|
| 41 |
# ---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def chat_with_gemini(prompt):
|
| 43 |
try:
|
| 44 |
response = gemini_model.generate_content(prompt)
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"❌ Error from Gemini: {str(e)}"
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# ---------------------------
|
| 50 |
# GRADIO APP UI
|
| 51 |
# ---------------------------
|
|
|
|
| 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 (Gemini Only)
|
| 85 |
with gr.TabItem("💬 Farmer's Chatbot"):
|
| 86 |
gr.Markdown("### Ask any question related to farming 👨🌾")
|
| 87 |
+
user_input = gr.Textbox(label="Your Question")
|
|
|
|
|
|
|
|
|
|
| 88 |
chatbot_output = gr.Textbox(label="AgroVision Bot Response")
|
| 89 |
+
chatbot_btn = gr.Button("Ask Gemini")
|
| 90 |
+
chatbot_btn.click(fn=chat_with_gemini, inputs=user_input, outputs=chatbot_output)
|
| 91 |
|
| 92 |
+
# Run the app
|
| 93 |
demo.launch()
|