Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,90 +5,76 @@ from oauth2client.service_account import ServiceAccountCredentials
|
|
5 |
from transformers import pipeline
|
6 |
from twilio.rest import Client
|
7 |
|
8 |
-
# Authenticate with Google Sheets
|
9 |
-
scope = [
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
"type": "service_account",
|
15 |
"project_id": "varahaa-farmers",
|
16 |
"private_key_id": "f48da20518b3a08df328b6d07006b35472445e5b",
|
17 |
-
"private_key": os.environ["
|
18 |
"client_email": "[email protected]",
|
19 |
"client_id": "113471167964788057428",
|
20 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
21 |
"token_uri": "https://oauth2.googleapis.com/token",
|
22 |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
23 |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/huggingface-bot%40varahaa-farmers.iam.gserviceaccount.com"
|
24 |
-
}
|
25 |
|
|
|
26 |
client = gspread.authorize(creds)
|
27 |
-
sheet = client.open("Varahaa Farmer Verification").sheet1
|
28 |
|
29 |
-
# Initialize Twilio
|
30 |
twilio_client = Client(os.environ["ACf8b3539472179ac15fb46939a02bb802"], os.environ["297cffb015681887dade0d42180332b9"])
|
31 |
|
32 |
# Load NLP model
|
33 |
-
qa_pipeline = pipeline("question-answering", model="bert-
|
34 |
|
35 |
def get_farmer_data(phone):
|
36 |
try:
|
37 |
-
|
38 |
-
return sheet.row_values(cell.row)
|
39 |
except:
|
40 |
return None
|
41 |
|
42 |
def verify_farmer(phone, query):
|
43 |
farmer = get_farmer_data(phone)
|
44 |
if not farmer:
|
45 |
-
return "Farmer not found
|
46 |
|
47 |
-
|
48 |
-
# [Phone, Name, SurveyDone, Address, FarmArea, Ownership, Signature, PaddyMethod, Residue, WheatTillage]
|
49 |
if "name" in query.lower():
|
50 |
-
return f"
|
51 |
elif "survey" in query.lower():
|
52 |
-
return f"Survey
|
53 |
elif "area" in query.lower():
|
54 |
-
return f"
|
55 |
-
elif "ownership" in query.lower():
|
56 |
-
return f"Ownership Status: {farmer[5]}"
|
57 |
-
elif "signature" in query.lower():
|
58 |
-
return f"Digital Signature: {'β
Verified' if farmer[6]=='Yes' else 'β Missing'}"
|
59 |
else:
|
60 |
-
return "
|
61 |
|
62 |
def send_questionnaire(phone):
|
63 |
try:
|
64 |
message = twilio_client.messages.create(
|
65 |
-
body="πΎ
|
66 |
-
|
67 |
-
"2. Has field staff completed survey?\n"
|
68 |
-
"3. Need ownership details updated?\n"
|
69 |
-
"Reply with answers separated by commas",
|
70 |
-
from_='whatsapp:+14155238886', # Twilio sandbox
|
71 |
to=f'whatsapp:{phone}'
|
72 |
)
|
73 |
-
return "
|
74 |
except Exception as e:
|
75 |
-
return f"
|
76 |
|
77 |
-
with gr.Blocks(
|
78 |
-
gr.Markdown("#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
output = gr.Textbox(label="Verification Result", interactive=False)
|
86 |
-
|
87 |
-
with gr.Row():
|
88 |
-
verify_btn = gr.Button("Verify Details", variant="primary")
|
89 |
-
questionnaire_btn = gr.Button("Send Questionnaire", variant="secondary")
|
90 |
|
91 |
verify_btn.click(fn=verify_farmer, inputs=[phone, query], outputs=output)
|
92 |
-
|
93 |
|
94 |
demo.launch()
|
|
|
5 |
from transformers import pipeline
|
6 |
from twilio.rest import Client
|
7 |
|
8 |
+
# Authenticate with Google Sheets
|
9 |
+
scope = [
|
10 |
+
"https://www.googleapis.com/auth/spreadsheets",
|
11 |
+
"https://www.googleapis.com/auth/drive"
|
12 |
+
]
|
13 |
|
14 |
+
creds_dict = {
|
15 |
"type": "service_account",
|
16 |
"project_id": "varahaa-farmers",
|
17 |
"private_key_id": "f48da20518b3a08df328b6d07006b35472445e5b",
|
18 |
+
"private_key": os.environ["PRIVATE_KEY"].replace('\\n', '\n'),
|
19 |
"client_email": "[email protected]",
|
20 |
"client_id": "113471167964788057428",
|
21 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
22 |
"token_uri": "https://oauth2.googleapis.com/token",
|
23 |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
24 |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/huggingface-bot%40varahaa-farmers.iam.gserviceaccount.com"
|
25 |
+
}
|
26 |
|
27 |
+
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
28 |
client = gspread.authorize(creds)
|
29 |
+
sheet = client.open("Varahaa Farmer Verification").sheet1
|
30 |
|
31 |
+
# Initialize Twilio
|
32 |
twilio_client = Client(os.environ["ACf8b3539472179ac15fb46939a02bb802"], os.environ["297cffb015681887dade0d42180332b9"])
|
33 |
|
34 |
# Load NLP model
|
35 |
+
qa_pipeline = pipeline("question-answering", model="bert-base-uncased")
|
36 |
|
37 |
def get_farmer_data(phone):
|
38 |
try:
|
39 |
+
return sheet.find(phone)
|
|
|
40 |
except:
|
41 |
return None
|
42 |
|
43 |
def verify_farmer(phone, query):
|
44 |
farmer = get_farmer_data(phone)
|
45 |
if not farmer:
|
46 |
+
return "Farmer not found"
|
47 |
|
48 |
+
row = sheet.row_values(farmer.row)
|
|
|
49 |
if "name" in query.lower():
|
50 |
+
return f"Name: {row[1]}"
|
51 |
elif "survey" in query.lower():
|
52 |
+
return f"Survey: {row[2]}"
|
53 |
elif "area" in query.lower():
|
54 |
+
return f"Farm Area: {row[4]} acres"
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
+
return "Available queries: name, survey, area"
|
57 |
|
58 |
def send_questionnaire(phone):
|
59 |
try:
|
60 |
message = twilio_client.messages.create(
|
61 |
+
body="πΎ Please verify your details:\n1. Is your name correct?\n2. Farm area?",
|
62 |
+
from_='whatsapp:+14155238886',
|
|
|
|
|
|
|
|
|
63 |
to=f'whatsapp:{phone}'
|
64 |
)
|
65 |
+
return "Questionnaire sent!"
|
66 |
except Exception as e:
|
67 |
+
return f"Error: {str(e)}"
|
68 |
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("# Varahaa Farmer Verification")
|
71 |
+
phone = gr.Textbox(label="Phone Number")
|
72 |
+
query = gr.Dropdown(["Name Check", "Survey Status", "Farm Area"], label="Select Query")
|
73 |
+
output = gr.Textbox(label="Result")
|
74 |
+
verify_btn = gr.Button("Verify")
|
75 |
+
send_btn = gr.Button("Send Questionnaire")
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
verify_btn.click(fn=verify_farmer, inputs=[phone, query], outputs=output)
|
78 |
+
send_btn.click(fn=send_questionnaire, inputs=phone, outputs=output)
|
79 |
|
80 |
demo.launch()
|