trapezius60 commited on
Commit
62c98c4
·
verified ·
1 Parent(s): 705dded

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from pyzbar.pyzbar import decode
3
+ from PIL import Image
4
+ import requests
5
+
6
+ # ===== Google Form URL & Entry IDs =====
7
+ FORM_URL = "https://docs.google.com/forms/u/1/d/e/1FAIpQLSd87wspuXaH0kcNZ33GOXsnCDVIFqaHQlpZin7FmSdFPJ4Q7Q/formResponse"
8
+
9
+ ENTRY_QR = "entry.2013313277"
10
+ ENTRY_LAT = "entry.286817265"
11
+ ENTRY_LNG = "entry.518488535"
12
+
13
+ # ===== Function scan QR + send GPS =====
14
+ def scan_qr_send_form(image, lat, lng):
15
+ img = Image.open(image)
16
+ result = decode(img)
17
+ if result:
18
+ qr_text = result[0].data.decode('utf-8')
19
+ data = {
20
+ ENTRY_QR: qr_text,
21
+ ENTRY_LAT: lat,
22
+ ENTRY_LNG: lng
23
+ }
24
+ r = requests.post(FORM_URL, data=data)
25
+ if r.status_code == 200:
26
+ return f"✅ Saved QR: {qr_text}\nLocation: {lat}, {lng}"
27
+ else:
28
+ return "❌ Failed to send to Form"
29
+ else:
30
+ return "❌ No QR code detected"
31
+
32
+ # ===== Gradio UI =====
33
+ with gr.Blocks() as demo:
34
+ gr.Markdown("### Scan QR Code & Auto GPS Location → Google Form")
35
+
36
+ qr_image = gr.Image(source="webcam", type="filepath", label="Scan QR Code")
37
+ lat_input = gr.Textbox(label="Latitude", interactive=False)
38
+ lng_input = gr.Textbox(label="Longitude", interactive=False)
39
+ output = gr.Textbox(label="Result")
40
+
41
+ btn = gr.Button("Scan & Submit")
42
+ btn.click(
43
+ scan_qr_send_form,
44
+ inputs=[qr_image, lat_input, lng_input],
45
+ outputs=output
46
+ )
47
+
48
+ # Custom JS to get GPS from mobile browser
49
+ demo.load(
50
+ """
51
+ <script>
52
+ if (navigator.geolocation) {
53
+ navigator.geolocation.getCurrentPosition(function(position) {
54
+ document.querySelectorAll('input[name="Latitude"]')[0].value = position.coords.latitude;
55
+ document.querySelectorAll('input[name="Longitude"]')[0].value = position.coords.longitude;
56
+ });
57
+ } else {
58
+ alert("Geolocation is not supported by this browser.");
59
+ }
60
+ </script>
61
+ """
62
+ )
63
+
64
+ demo.launch()