jays009 commited on
Commit
dd0dfd1
·
verified ·
1 Parent(s): a53a305

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -17
app.py CHANGED
@@ -7,10 +7,15 @@ from huggingface_hub import hf_hub_download
7
  from PIL import Image
8
  import requests
9
  from io import BytesIO
 
 
10
 
11
  # Define the number of classes
12
  num_classes = 2
13
 
 
 
 
14
  # Download model from Hugging Face
15
  def download_model():
16
  model_path = hf_hub_download(repo_id="jays009/Restnet50", filename="pytorch_model.bin")
@@ -79,7 +84,7 @@ def predict_from_url(url):
79
  print(f"Error during URL processing: {e}")
80
  return {"error": f"Failed to process the URL: {str(e)}"}
81
 
82
- # Gradio interface with logging
83
  def predict(image, url):
84
  try:
85
  if image:
@@ -89,27 +94,66 @@ def predict(image, url):
89
  else:
90
  result = {"error": "No input provided. Please upload an image or provide a URL."}
91
 
 
 
 
 
92
  # Log the result
93
- event_id = id(result) # Generate a simple event ID for logging purposes
94
  print(f"Event ID: {event_id}, Result: {result}")
95
- return result
96
 
97
  except Exception as e:
98
  print(f"Error in prediction function: {e}")
99
  return {"error": str(e)}
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Gradio interface setup
102
- iface = gr.Interface(
103
- fn=predict,
104
- inputs=[
105
- gr.Image(type="pil", label="Upload an Image"),
106
- gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL"),
107
- ],
108
- outputs=gr.JSON(label="Prediction Result"),
109
- live=True,
110
- title="Maize Anomaly Detection",
111
- description="Upload an image or provide a URL to detect anomalies in maize crops.",
112
- )
113
-
114
- # Launch the interface
115
- iface.launch(share=True, show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  from PIL import Image
8
  import requests
9
  from io import BytesIO
10
+ from fastapi import FastAPI
11
+ from gradio.routes import App
12
 
13
  # Define the number of classes
14
  num_classes = 2
15
 
16
+ # In-memory storage for results
17
+ results_cache = {}
18
+
19
  # Download model from Hugging Face
20
  def download_model():
21
  model_path = hf_hub_download(repo_id="jays009/Restnet50", filename="pytorch_model.bin")
 
84
  print(f"Error during URL processing: {e}")
85
  return {"error": f"Failed to process the URL: {str(e)}"}
86
 
87
+ # Main prediction function with caching
88
  def predict(image, url):
89
  try:
90
  if image:
 
94
  else:
95
  result = {"error": "No input provided. Please upload an image or provide a URL."}
96
 
97
+ # Generate and store the event ID
98
+ event_id = id(result) # Use Python's id() function to generate a unique identifier
99
+ results_cache[event_id] = result
100
+
101
  # Log the result
 
102
  print(f"Event ID: {event_id}, Result: {result}")
103
+ return {"event_id": event_id, "result": result}
104
 
105
  except Exception as e:
106
  print(f"Error in prediction function: {e}")
107
  return {"error": str(e)}
108
 
109
+ # Function to retrieve result by event_id
110
+ def get_result(event_id):
111
+ try:
112
+ # Convert event_id from string to int
113
+ event_id = int(event_id)
114
+ result = results_cache.get(event_id)
115
+ if result:
116
+ return result
117
+ else:
118
+ return {"error": "No result found for the provided event ID."}
119
+ except Exception as e:
120
+ return {"error": f"Invalid event ID: {str(e)}"}
121
+
122
+ # Create a FastAPI app for handling the GET request
123
+ app = FastAPI()
124
+
125
+ @app.get("/result/{event_id}")
126
+ def get_result_api(event_id: int):
127
+ return get_result(event_id)
128
+
129
  # Gradio interface setup
130
+ iface = gr.Blocks()
131
+
132
+ with iface:
133
+ gr.Markdown("# Maize Anomaly Detection")
134
+ with gr.Row():
135
+ image_input = gr.Image(type="pil", label="Upload an Image")
136
+ url_input = gr.Textbox(label="Or Enter an Image URL", placeholder="Provide a valid image URL")
137
+ output = gr.JSON(label="Prediction Result")
138
+ submit_button = gr.Button("Submit")
139
+
140
+ submit_button.click(
141
+ fn=predict,
142
+ inputs=[image_input, url_input],
143
+ outputs=output
144
+ )
145
+
146
+ # Event ID retrieval section
147
+ with gr.Row():
148
+ event_id_input = gr.Textbox(label="Event ID", placeholder="Enter Event ID")
149
+ event_output = gr.JSON(label="Retrieved Result")
150
+ retrieve_button = gr.Button("Get Result")
151
+
152
+ retrieve_button.click(
153
+ fn=get_result,
154
+ inputs=[event_id_input],
155
+ outputs=event_output
156
+ )
157
+
158
+ # Launch the Gradio interface
159
+ iface.launch(share=True, show_error=True, server_name="0.0.0.0", server_port=7860)