Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient(model="openai/gpt-oss-20b", provider="auto")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import requests
|
| 4 |
+
import torch
|
| 5 |
import gradio as gr
|
| 6 |
+
from transformers import AutoTokenizer, pipeline
|
| 7 |
|
| 8 |
+
# --- Model Setup
|
| 9 |
+
MODEL_ID = "Aaayushiii/mt5-crop-lora"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
|
|
|
| 11 |
|
| 12 |
+
# Prefer GPU if available
|
| 13 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 14 |
|
| 15 |
+
# Build the pipeline (text2text for mT5)
|
| 16 |
+
pipe = pipeline(
|
| 17 |
+
"text2text-generation",
|
| 18 |
+
model=MODEL_ID,
|
| 19 |
+
tokenizer=tokenizer,
|
| 20 |
+
device=device,
|
| 21 |
+
)
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# --- Weather (OpenWeather key or fallback)
|
| 24 |
+
OPENWEATHER_API_KEY = os.getenv("OPENWEATHER_API_KEY", "").strip()
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def get_weather(location: str) -> str | None:
|
| 27 |
+
if OPENWEATHER_API_KEY:
|
| 28 |
+
try:
|
| 29 |
+
url = (
|
| 30 |
+
"http://api.openweathermap.org/data/2.5/weather"
|
| 31 |
+
f"?q={location}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 32 |
+
)
|
| 33 |
+
r = requests.get(url, timeout=10).json()
|
| 34 |
+
if r.get("cod") == 200:
|
| 35 |
+
return f"📍 {r['name']} | 🌡️ {r['main']['temp']}°C | {r['weather'][0]['description'].capitalize()}"
|
| 36 |
+
except Exception:
|
| 37 |
+
pass
|
| 38 |
+
# Fallback Open-Meteo
|
| 39 |
+
try:
|
| 40 |
+
geo = requests.get(
|
| 41 |
+
f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1",
|
| 42 |
+
timeout=10,
|
| 43 |
+
).json()
|
| 44 |
+
if not geo.get("results"):
|
| 45 |
+
return None
|
| 46 |
+
lat, lon = geo["results"][0]["latitude"], geo["results"][0]["longitude"]
|
| 47 |
+
city = geo["results"][0]["name"]
|
| 48 |
+
wx = requests.get(
|
| 49 |
+
f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true",
|
| 50 |
+
timeout=10,
|
| 51 |
+
).json()
|
| 52 |
+
cw = wx.get("current_weather")
|
| 53 |
+
if cw:
|
| 54 |
+
return f"📍 {city} | 🌡️ {cw['temperature']}°C | 💨 {cw['windspeed']} km/h"
|
| 55 |
+
except Exception:
|
| 56 |
+
pass
|
| 57 |
+
return None
|
| 58 |
|
| 59 |
+
def extract_location(text: str) -> str | None:
|
| 60 |
+
caps = re.findall(r"\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b", text)
|
| 61 |
+
return max(caps, key=len) if caps else None
|
| 62 |
|
| 63 |
+
def crop_recommendation(query: str, weather: str | None = None) -> str:
|
| 64 |
+
prompt = f"User: {query}\n"
|
| 65 |
+
if weather:
|
| 66 |
+
prompt += f"(Weather: {weather})\n"
|
| 67 |
+
prompt += "Assistant: Provide crop recommendation including top options, weather suitability, and profitability."
|
| 68 |
+
out = pipe(
|
| 69 |
+
prompt,
|
| 70 |
+
max_new_tokens=150,
|
| 71 |
+
num_beams=3,
|
| 72 |
+
do_sample=True,
|
| 73 |
+
temperature=0.7,
|
| 74 |
+
)[0]["generated_text"]
|
| 75 |
+
return out
|
| 76 |
|
| 77 |
+
def agent(query: str) -> str:
|
| 78 |
+
loc = extract_location(query)
|
| 79 |
+
if "weather" in query.lower():
|
| 80 |
+
if not loc:
|
| 81 |
+
return "⚠️ Please specify a location."
|
| 82 |
+
return get_weather(loc) or f"⚠️ Weather not found for {loc}."
|
| 83 |
+
if loc:
|
| 84 |
+
weather = get_weather(loc)
|
| 85 |
+
return crop_recommendation(query, weather)
|
| 86 |
+
return crop_recommendation(query)
|
| 87 |
|
| 88 |
+
# --- UI
|
| 89 |
+
img_path = "assets/crop.png"
|
| 90 |
+
examples = [
|
| 91 |
+
"Weather in Delhi",
|
| 92 |
+
"Best crop in Agra during Kharif",
|
| 93 |
+
"What should I sow in Maharashtra this Rabi?",
|
| 94 |
+
]
|
| 95 |
|
| 96 |
+
with gr.Blocks(css="""
|
| 97 |
+
body {
|
| 98 |
+
background: linear-gradient(to right bottom, #d0f0e0, #f7fff7);
|
| 99 |
+
font-family: 'Inter', sans-serif;
|
| 100 |
+
color: #1b4332;
|
| 101 |
+
display: flex;
|
| 102 |
+
justify-content: center;
|
| 103 |
+
}
|
| 104 |
+
.main-card {
|
| 105 |
+
max-width: 800px;
|
| 106 |
+
margin: 40px auto;
|
| 107 |
+
padding: 24px;
|
| 108 |
+
background: rgba(255, 255, 255, 0.75);
|
| 109 |
+
border-radius: 20px;
|
| 110 |
+
box-shadow: 0 8px 30px rgba(0,0,0,0.12);
|
| 111 |
+
backdrop-filter: blur(12px);
|
| 112 |
+
border: 1px solid rgba(255,255,255,0.3);
|
| 113 |
+
}
|
| 114 |
+
.chatbox {
|
| 115 |
+
background: rgba(255, 255, 255, 0.65);
|
| 116 |
+
border-radius: 16px;
|
| 117 |
+
padding: 12px;
|
| 118 |
+
border: 1px solid rgba(167,201,87,0.4);
|
| 119 |
+
min-height: 350px;
|
| 120 |
+
backdrop-filter: blur(8px);
|
| 121 |
+
}
|
| 122 |
+
.gr-button {
|
| 123 |
+
background: linear-gradient(to right bottom, #6a994e, #a7c957) !important;
|
| 124 |
+
color: white !important;
|
| 125 |
+
border-radius: 14px;
|
| 126 |
+
font-weight: 600;
|
| 127 |
+
padding: 10px 18px;
|
| 128 |
+
transition: all 0.25s ease-in-out;
|
| 129 |
+
margin-top: 10px;
|
| 130 |
+
}
|
| 131 |
+
.gr-button:hover {
|
| 132 |
+
transform: translateY(-2px) scale(1.05);
|
| 133 |
+
box-shadow: 0 0 12px rgba(106,153,78,0.6);
|
| 134 |
+
}
|
| 135 |
+
.gr-textbox textarea {
|
| 136 |
+
border: 2px solid #a7c957;
|
| 137 |
+
border-radius: 14px;
|
| 138 |
+
padding: 12px;
|
| 139 |
+
font-size: 15px;
|
| 140 |
+
background: rgba(255,255,255,0.8);
|
| 141 |
+
backdrop-filter: blur(6px);
|
| 142 |
+
}
|
| 143 |
+
h1 {
|
| 144 |
+
font-size: 32px;
|
| 145 |
+
font-weight: 700;
|
| 146 |
+
margin-bottom: 20px;
|
| 147 |
+
text-align: center;
|
| 148 |
+
background: linear-gradient(to right bottom, #2f5233, #588157);
|
| 149 |
+
-webkit-background-clip: text;
|
| 150 |
+
-webkit-text-fill-color: transparent;
|
| 151 |
+
}
|
| 152 |
+
#example-row .examples {
|
| 153 |
+
display: flex;
|
| 154 |
+
justify-content: center;
|
| 155 |
+
flex-wrap: wrap;
|
| 156 |
+
gap: 10px;
|
| 157 |
+
margin: 15px 0 20px 0;
|
| 158 |
+
}
|
| 159 |
+
#example-row .example {
|
| 160 |
+
border-radius: 20px;
|
| 161 |
+
background: linear-gradient(to right bottom, #6a994e, #a7c957);
|
| 162 |
+
color: #fff;
|
| 163 |
+
font-size: 14px;
|
| 164 |
+
padding: 6px 16px;
|
| 165 |
+
cursor: pointer;
|
| 166 |
+
transition: all 0.25s ease;
|
| 167 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 168 |
+
}
|
| 169 |
+
#example-row .example:hover {
|
| 170 |
+
background: linear-gradient(to right bottom, #588157, #6a994e);
|
| 171 |
+
transform: translateY(-2px) scale(1.06);
|
| 172 |
+
box-shadow: 0 0 15px rgba(106,153,78,0.7);
|
| 173 |
+
}
|
| 174 |
+
""") as demo:
|
| 175 |
+
with gr.Column(elem_classes="main-card"):
|
| 176 |
+
gr.Markdown("<h1>🌱 AgriBot – Crop + Weather Assistant</h1>")
|
| 177 |
+
chatbot = gr.Chatbot(label="Chat with AgriBot", elem_classes="chatbox", height=400)
|
| 178 |
+
user_in = gr.Textbox(placeholder="Ask something, e.g., 'Weather in Jaipur?'")
|
| 179 |
+
gr.Examples(examples=examples, inputs=user_in, elem_id="example-row")
|
| 180 |
+
submit = gr.Button("Ask")
|
| 181 |
+
|
| 182 |
+
if os.path.exists(img_path):
|
| 183 |
+
gr.Image(img_path, show_label=False)
|
| 184 |
+
gr.Markdown("""
|
| 185 |
+
### 🍀 Features
|
| 186 |
+
- 🌦️ Live weather lookup
|
| 187 |
+
- 🌾 Crop recommendations
|
| 188 |
+
- 💰 Profitability insights
|
| 189 |
+
""")
|
| 190 |
|
| 191 |
+
def respond(history, msg):
|
| 192 |
+
reply = agent(msg)
|
| 193 |
+
history.append((msg, reply))
|
| 194 |
+
return history, None
|
| 195 |
+
|
| 196 |
+
submit.click(respond, [chatbot, user_in], [chatbot, user_in])
|
| 197 |
+
user_in.submit(respond, [chatbot, user_in], [chatbot, user_in])
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|
| 200 |
+
# Works both locally (no localhost access -> share link) and on Spaces
|
| 201 |
+
port = int(os.getenv("PORT", "7860"))
|
| 202 |
+
share_flag = os.getenv("GRADIO_SHARE", "true").lower() in {"1", "true", "yes"}
|
| 203 |
+
demo.queue(concurrency_count=4).launch(
|
| 204 |
+
server_name="0.0.0.0",
|
| 205 |
+
server_port=port,
|
| 206 |
+
share=share_flag,
|
| 207 |
+
show_error=True,
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
|