Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import requests | |
| API_KEY = os.getenv("GROQ_API_KEY", "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") | |
| def ask_kisanai(question): | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| system_prompt = "You are KisanAIAdvisor for Pakistani farmers. Give practical agriculture advice in simple language." | |
| data = { | |
| "model": "llama-3.1-8b-instant", | |
| "messages": [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": question} | |
| ], | |
| "temperature": 0.3, | |
| "max_tokens": 300 | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()['choices'][0]['message']['content'] | |
| else: | |
| return f"API Error: {response.status_code}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Simple interface | |
| demo = gr.Interface( | |
| fn=ask_kisanai, | |
| inputs=gr.Textbox(label="Ask about agriculture", placeholder="Type in Urdu or English..."), | |
| outputs=gr.Textbox(label="Answer"), | |
| title="🌾 KisanAIAdvisor", | |
| examples=[ | |
| ["What crops grow best in Punjab?"], | |
| ["Meri fasal mein keera lag gaya hai"], | |
| ["How to test soil quality?"] | |
| ] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |