Update app.py
Browse files5 unmodified lines678910111213141516171819202122234 unmodified lines282930313233343 unmodified lines38394041424344454647484950515253545 unmodified linesst.markdown("""<h1 style='text-align: center; color: green;'>🌾 مقامی زبان ایگری-ایڈوائزری چیٹ بوٹ 🌾</h1><p style='text-align: center;'>زراعت کے متعلق سوال کریں: فصل، کھاد، کیڑے، موسم، منڈی کی قیمتیں</p>""", unsafe_allow_html=True)# Fallback: Use environment variable or direct key if secrets not foundtry:GROQ_API_KEY = st.secrets["GROQ_API_KEY"]except Exception:GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your-groq-api-key") # fallbackdef ask_groq(prompt):if GROQ_API_KEY == "your-groq-api-key":return "GROQ API key missing! Please set it via `.streamlit/secrets.toml` or environment variable."url = "https://api.groq.com/openai/v1/chat/completions"headers = {4 unmodified linespayload = {"model": "mixtral-8x7b-32768","messages": [{"role": "system", "content": "You are an agricultural advisor who replies in Urdu, Pashto, or Sindhi. Give simple, relevant advice for local farmers on crops, weather, and pests."},{"role": "user", "content": prompt}]}3 unmodified linesresponse.raise_for_status()return response.json()['choices'][0]['message']['content']except Exception as e:return "جواب حاصل کرنے میں مسئلہ پیش آیا۔ براہ کرم انٹرنیٹ کنکشن یا API کلید چیک کریں۔"user_query = st.text_area("اپنا سوال یہاں لکھیں", placeholder="مثال: کپاس کی فصل کو کیڑوں سے بچانے کا طریقہ؟", height=100)if st.button("سوال پوچھیں"):if user_query.strip() != "":with st.spinner("جواب تیار ہو رہا ہے..."):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)else:st.warning("براہ کرم سوال درج کریں۔")st.markdown("<br><hr><center>Developed for Farmers of Pakistan</center>", unsafe_allow_html=True)5 unmodified lines678910111213141516171819204 unmodified lines252627282930313 unmodified lines3536373839404142434445464748495051525 unmodified linesst.markdown("""<h1 style='text-align: center; color: green;'>🌾 مقامی زبان ایگری-ایڈوائزری چیٹ بوٹ 🌾</h1><p style='text-align: center;'>فصل، کھاد، موسم، یا منڈی کی قیمتوں سے متعلق سوال کریں۔</p>""", unsafe_allow_html=True)# Set GROQ API key using environment variable (recommended for Hugging Face)GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your-groq-api-key")def ask_groq(prompt):if GROQ_API_KEY == "your-groq-api-key":return "GROQ API key missing! Please set GROQ_API_KEY in the environment variables."url = "https://api.groq.com/openai/v1/chat/completions"headers = {4 unmodified linespayload = {"model": "mixtral-8x7b-32768","messages": [{"role": "system", "content": "You are a helpful agriculture assistant who replies in the user's language (Urdu, Pashto, or Sindhi). Keep answers short, clear, and practical for farmers."},{"role": "user", "content": prompt}]}3 unmodified linesresponse.raise_for_status()return response.json()['choices'][0]['message']['content']except Exception as e:return "سروس میں مسئلہ ہے۔ براہ کرم دوبارہ کوشش کریں۔"# User inputquery = st.text_area("سوال لکھیں", placeholder="مثال: آلو کی فصل کی بوائی کا بہترین وقت؟", height=100)if st.button("سوال پوچھیں"):if query.strip():with st.spinner("جواب تیار ہو رہا ہے..."):answer = ask_groq(query)st.success("ماہر کا مشورہ:")st.markdown(f"<div style='background-color:#f0fff0;padding:15px;border-radius:10px;'>{answer}</div>", unsafe_allow_html=True)else:st.warning("براہ کرم سوال درج کریں۔")st.markdown("<hr><center>Developed for Pakistani Farmers - Powered by GROQ API</center>", unsafe_allow_html=True)
app.py+13-15