caviri commited on
Commit
f555cdb
·
1 Parent(s): b168dc3
Files changed (3) hide show
  1. Dockerfile +20 -0
  2. main.py +76 -0
  3. requirements.txt +1 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ COPY . /app
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Make port 7860 available to the world outside this container
14
+ EXPOSE 7860
15
+
16
+ # Define environment variable
17
+ ENV PYTHONUNBUFFERED=1
18
+
19
+ # Run app.py when the container launches
20
+ CMD ["python", "main.py"]
main.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+
4
+ def get_location(location_data):
5
+ if location_data is None:
6
+ return "Location access denied or unavailable"
7
+
8
+ try:
9
+ # Parse the location data JSON string
10
+ location = json.loads(location_data)
11
+ latitude = location.get("latitude")
12
+ longitude = location.get("longitude")
13
+ accuracy = location.get("accuracy")
14
+
15
+ result = f"""
16
+ Location Details:
17
+ ----------------
18
+ Latitude: {latitude}
19
+ Longitude: {longitude}
20
+ Accuracy: {accuracy} meters
21
+ """
22
+ return result
23
+ except Exception as e:
24
+ return f"Error processing location data: {str(e)}"
25
+
26
+ # Create the Gradio interface
27
+ with gr.Blocks(js="""
28
+ async () => {
29
+ if ("geolocation" in navigator) {
30
+ try {
31
+ const position = await new Promise((resolve, reject) => {
32
+ navigator.geolocation.getCurrentPosition(resolve, reject);
33
+ });
34
+
35
+ document.querySelector("#location_data").value = JSON.stringify({
36
+ latitude: position.coords.latitude,
37
+ longitude: position.coords.longitude,
38
+ accuracy: position.coords.accuracy
39
+ });
40
+ } catch (error) {
41
+ document.querySelector("#location_data").value = JSON.stringify({
42
+ error: "Failed to get location: " + error.message
43
+ });
44
+ }
45
+ } else {
46
+ document.querySelector("#location_data").value = JSON.stringify({
47
+ error: "Geolocation is not supported by this browser/device"
48
+ });
49
+ }
50
+ }
51
+ """) as app:
52
+ gr.Markdown("# GPS Location Demo")
53
+ gr.Markdown("Click the button below to share your location")
54
+
55
+ # Add location input component with specific ID for JavaScript
56
+ location_data = gr.JSON(label="Location Data", elem_id="location_data", visible=False)
57
+
58
+ # Add button to trigger location request
59
+ button = gr.Button("Get My Location")
60
+
61
+ # Add output text area
62
+ output = gr.Textbox(label="Location Details", lines=6)
63
+
64
+ # Connect components
65
+ button.click(
66
+ fn=get_location,
67
+ inputs=[location_data],
68
+ outputs=[output]
69
+ )
70
+
71
+ # Launch the app
72
+ if __name__ == "__main__":
73
+ app.launch(
74
+ server_name="0.0.0.0", # Important: Listen on all network interfaces
75
+ share=False
76
+ )
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio