Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# Load farmer data from CSV
|
6 |
+
farmers_df = pd.read_csv("farmers.csv")
|
7 |
+
|
8 |
+
# Load pre-trained NLP models (free)
|
9 |
+
qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
|
10 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
11 |
+
|
12 |
+
def get_farmer_context(phone):
|
13 |
+
farmer = farmers_df[farmers_df["Phone"] == phone]
|
14 |
+
if farmer.empty:
|
15 |
+
return None
|
16 |
+
return farmer.iloc[0].to_string()
|
17 |
+
|
18 |
+
def verify_details(phone, query):
|
19 |
+
context = get_farmer_context(phone)
|
20 |
+
if not context:
|
21 |
+
return "Farmer not found. Check your phone number."
|
22 |
+
|
23 |
+
# Handle specific queries
|
24 |
+
if "survey" in query.lower():
|
25 |
+
answer = qa_pipeline(question="Is the survey done?", context=context)['answer']
|
26 |
+
return f"Survey Status: {answer}"
|
27 |
+
elif "area" in query.lower():
|
28 |
+
answer = qa_pipeline(question="What is the farm area?", context=context)['answer']
|
29 |
+
return f"Farm Area: {answer} acres"
|
30 |
+
elif "ownership" in query.lower():
|
31 |
+
answer = qa_pipeline(question="What is the ownership status?", context=context)['answer']
|
32 |
+
return f"Ownership: {answer}"
|
33 |
+
elif "paddy" in query.lower():
|
34 |
+
answer = qa_pipeline(question="Which paddy method is used?", context=context)['answer']
|
35 |
+
return f"Paddy Method: {answer}"
|
36 |
+
elif "wheat" in query.lower():
|
37 |
+
answer = qa_pipeline(question="Wheat tillage method?", context=context)['answer']
|
38 |
+
return f"Wheat Tillage: {answer}"
|
39 |
+
else:
|
40 |
+
return "Ask about: survey, area, ownership, paddy, wheat."
|
41 |
+
|
42 |
+
def classify_practice(text):
|
43 |
+
result = classifier(text)[0]
|
44 |
+
return f"Classification: {result['label']} (Confidence: {round(result['score']*100)}%)"
|
45 |
+
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("# 🌾 Varahaa Farmer Verification Bot")
|
48 |
+
|
49 |
+
with gr.Tab("Verify Farmer Details"):
|
50 |
+
phone = gr.Textbox(label="Enter Phone Number (+91XXXXXXXXXX)")
|
51 |
+
query = gr.Textbox(label="Ask a question (e.g., 'Survey status?')")
|
52 |
+
output = gr.Textbox(label="Response")
|
53 |
+
verify_btn = gr.Button("Verify")
|
54 |
+
verify_btn.click(fn=verify_details, inputs=[phone, query], outputs=output)
|
55 |
+
|
56 |
+
with gr.Tab("Classify Farming Practice"):
|
57 |
+
text = gr.Textbox(label="Describe your practice (e.g., 'I use DSR')")
|
58 |
+
cls_output = gr.Textbox(label="Result")
|
59 |
+
cls_btn = gr.Button("Classify")
|
60 |
+
cls_btn.click(fn=classify_practice, inputs=text, outputs=cls_output)
|
61 |
+
|
62 |
+
demo.launch()
|