File size: 1,480 Bytes
53c1521
d0adf98
929bb9f
53c1521
929bb9f
d0adf98
929bb9f
 
 
 
 
 
53c1521
929bb9f
53c1521
929bb9f
 
 
 
 
53c1521
929bb9f
 
 
53c1521
929bb9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53c1521
 
929bb9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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()