Download app.py from Ai1996/Farming: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/spaces/Ai1996/Farming/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ai1996/Farming/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ai1996/Farming/resolve/main/app.py
2.25 kB
| import streamlit as st | |
| import requests | |
| import os | |
| st.set_page_config(page_title="Agri Advisory Chatbot", page_icon="🌾", layout="centered") | |
| st.markdown(""" | |
| <h1 style='text-align: center; color: green;'>🌾 مقامی زبان ایگری-ایڈوائزری چیٹ بوٹ 🌾</h1> | |
| <p style='text-align: center;'>زراعت کے متعلق سوال کریں: فصل، کھاد، کیڑے، موسم، منڈی کی قیمتیں</p> | |
| """, unsafe_allow_html=True) | |
| # Set your GROQ API key here | |
| GROQ_API_KEY = st.secrets.get("GROQ_API_KEY", "your-groq-api-key") # Replace with your key or use secrets | |
| def ask_groq(prompt): | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "model": "mixtral-8x7b-32768", | |
| "messages": [ | |
| {"role": "system", "content": "You are an agricultural advisor who replies in the same language as the farmer (Urdu, Pashto, Sindhi). Give simple and useful farming advice about crop care, pest control, weather, and market rates."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| } | |
| response = requests.post(url, headers=headers, json=payload) | |
| return response.json()['choices'][0]['message']['content'] | |
| # Input section | |
| user_query = st.text_area("اپنا سوال یہاں لکھیں", placeholder="مثال: گندم کی بوائی کا بہترین وقت کیا ہے؟", height=100) | |
| if st.button("سوال پوچھیں"): | |
| if user_query.strip() != "": | |
| with st.spinner("جواب تیار ہو رہا ہے..."): | |
| try: | |
| reply = ask_groq(user_query) | |
| st.success("ماہر زرعی مشورہ:") | |
| st.markdown(f"<div style='background-color:#f0fff0;padding:15px;border-radius:10px;'>{reply}</div>", unsafe_allow_html=True) | |
| except Exception as e: | |
| st.error("معذرت، جواب حاصل کرنے میں مسئلہ پیش آیا۔ دوبارہ کوشش کریں۔") | |
| else: | |
| st.warning("براہ کرم سوال درج کریں۔") | |
| st.markdown("<br><hr><center>Developed for Farmers of Pakistan</center>", unsafe_allow_html=True) |