backendrmitem / app.py
iacelectricals's picture
Update app.py
97929d4 verified
Raw
History Blame Contribute Delete
3.89 kB
from flask import Flask, request, jsonify
import requests
import gspread
import os
app = Flask(__name__)
HF_TOKEN = os.getenv("HF_API_KEY")
# Authenticate with Google Sheets
gc = gspread.service_account(filename="credentials.json")
sheet = gc.open_by_url("https://docs.google.com/spreadsheets/d/1t4cVCDInque373CHEeMaNuwi8pGnAELqHJSPRLG3bKk/edit").worksheet("Items QR Code")
sheetStore = gc.open_by_url("https://docs.google.com/spreadsheets/d/11fLYMW3HSogJKrvawn6okMrau2JJ1FMToMVl2g0-ubU/edit").worksheet("Items QR Code")
sheetForms = gc.open_by_url("https://docs.google.com/spreadsheets/d/10U09hJsay6osJWTtPD1s3XjaOl6jCJVojdgfMYbNtu0/edit").worksheet("HR FORM")
# Token for Hugging Face Space
@app.route('/items', methods=['GET'])
def get_item():
item_description = request.args.get('description')
print(item_description)
if not item_description:
return jsonify({"error": "Missing item description"}), 400
# Search for the item in the Google Sheet
data = sheet.get_all_records()
for row in data:
if row["ItemDescription"] == item_description:
return jsonify(row)
print("result" + row)
# If not found, proxy the request to Hugging Face Space
hf_response = requests.get(
f"https://iacelectricals-backendstoreitem.hf.space/items?description={item_description}",
headers={"Authorization": f"Bearer {HF_TOKEN}"}
)
if hf_response.status_code == 200:
return hf_response.json()
else:
return jsonify({"error": "Item not found in Hugging Face Space"}), 404
@app.route('/storeItems', methods=['GET'])
def get_store_item():
item_description = request.args.get('description')
print(item_description)
if not item_description:
return jsonify({"error": "Missing item description"}), 400
# Search for the item in the Google Sheet
data = sheetStore.get_all_records()
for row in data:
if row["ItemDescription"] == item_description:
return jsonify(row)
print("result" + row)
# If not found, proxy the request to Hugging Face Space
hf_response = requests.get(
f"https://iacelectricals-backendstoreitem.hf.space/items?description={item_description}",
headers={"Authorization": f"Bearer {HF_TOKEN}"}
)
if hf_response.status_code == 200:
return hf_response.json()
else:
return jsonify({"error": "Item not found in Hugging Face Space"}), 404
@app.route('/getForms', methods=['GET'])
def get_all_forms():
try:
# Fetch all rows from Google Sheet starting from row 2
data = sheetForms.get_all_records()
forms = []
for row in data:
forms.append({
"formName": row["USER FORMS"], # Column A
"formLink": row["LINKS"], # Column B
"formDescription": row["DESCRIPTION"] # Column C
})
return jsonify(forms)
except Exception as e:
return jsonify({"error": str(e)}), 500
# def get_forms():
# form_name = request.args.get('formName')
# if not form_name:
# return jsonify({"error": "Missing form name"}), 400
# # Fetch all rows from Google Sheet
# data = sheetForms.get_all_records()
# for row in data:
# if row["USER FORMS"] == form_name:
# return jsonify(row)
# # If form not found in Google Sheet, proxy the request to Hugging Face Space
# hf_response = requests.get(
# f"https://iacelectricals-backendstoreitem.hf.space/forms?formName={form_name}",
# headers={"Authorization": f"Bearer {HF_TOKEN}"}
# )
# if hf_response.status_code == 200:
# return hf_response.json()
# else:
# return jsonify({"error": "Form not found in Hugging Face Space"}), 404
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)