noddysnots's picture
Update app.py
bf4a0ea verified
import gradio as gr
import gspread
import os
from google.oauth2 import service_account
from twilio.rest import Client
# Google Sheets Setup
scope = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive"
]
private_key = os.environ["PRIVATE_KEY"].replace('\\n', '\n')
credentials = service_account.Credentials.from_service_account_info({
"type": "service_account",
"project_id": "varahaa-farmers",
"private_key_id": "f48da20518b3a08df328b6d07006b35472445e5b",
"private_key": private_key,
"client_email": "[email protected]",
"client_id": "113471167964788057428",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/huggingface-bot%40varahaa-farmers.iam.gserviceaccount.com"
}, scopes=scope)
gc = gspread.authorize(credentials)
sheet = gc.open("Varahaa Farmer Verification").sheet1
# Rest of your Twilio and Gradio code...
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
client = gspread.authorize(creds)
sheet = client.open("Varahaa Farmer Verification").sheet1
# Initialize Twilio
twilio_client = Client(os.environ["ACf8b3539472179ac15fb46939a02bb802"], os.environ["297cffb015681887dade0d42180332b9"])
# Load NLP model
qa_pipeline = pipeline("question-answering", model="bert-base-uncased")
def get_farmer_data(phone):
try:
return sheet.find(phone)
except:
return None
def verify_farmer(phone, query):
farmer = get_farmer_data(phone)
if not farmer:
return "Farmer not found"
row = sheet.row_values(farmer.row)
if "name" in query.lower():
return f"Name: {row[1]}"
elif "survey" in query.lower():
return f"Survey: {row[2]}"
elif "area" in query.lower():
return f"Farm Area: {row[4]} acres"
else:
return "Available queries: name, survey, area"
def send_questionnaire(phone):
try:
message = twilio_client.messages.create(
body="๐ŸŒพ Please verify your details:\n1. Is your name correct?\n2. Farm area?",
from_='whatsapp:+14155238886',
to=f'whatsapp:{phone}'
)
return "Questionnaire sent!"
except Exception as e:
return f"Error: {str(e)}"
with gr.Blocks() as demo:
gr.Markdown("# Varahaa Farmer Verification")
phone = gr.Textbox(label="Phone Number")
query = gr.Dropdown(["Name Check", "Survey Status", "Farm Area"], label="Select Query")
output = gr.Textbox(label="Result")
verify_btn = gr.Button("Verify")
send_btn = gr.Button("Send Questionnaire")
verify_btn.click(fn=verify_farmer, inputs=[phone, query], outputs=output)
send_btn.click(fn=send_questionnaire, inputs=phone, outputs=output)
demo.launch()