ehtyalee commited on
Commit
190598a
·
verified ·
1 Parent(s): 9abf394

Upload 2 files

Browse files
Files changed (2) hide show
  1. main.py +18 -30
  2. requirements.txt +7 -5
main.py CHANGED
@@ -98,35 +98,23 @@ class ImagePredictor:
98
  raise RuntimeError("Model loading failed.") from e
99
 
100
  # --- Prediction Method ---
101
- def predict(self, image: Image.Image):
102
- """
103
- Takes a PIL image, preprocesses it, and returns label probabilities.
104
- Uses the loaded instance attributes (self.model, self.transforms, etc.)
105
- """
106
- # Check if initialization succeeded (should be caught by __init__ exceptions, but good practice)
107
- if self.model is None or self.transforms is None or self.id2label is None:
108
- return {"Error": "Predictor not initialized correctly. Check logs."}
109
- if image is None:
110
- return None # Gradio handles None input
111
-
112
- try:
113
- # Preprocess the image
114
- image = image.convert("RGB") # Ensure 3 channels
115
- pixel_values = self.transforms(image).unsqueeze(0).to(self.device)
116
-
117
- # Perform inference
118
- with torch.no_grad():
119
- outputs = self.model(pixel_values=pixel_values)
120
- logits = outputs.logits
121
-
122
- # Get probabilities and format output
123
- probabilities = F.softmax(logits, dim=-1)[0] # Get probabilities for the first image
124
- confidences = {self.id2label[i]: float(prob) for i, prob in enumerate(probabilities)}
125
- return confidences
126
-
127
- except Exception as e:
128
- print(f"Error during prediction: {e}")
129
- return {"Error": f"Prediction failed: {str(e)}"}
130
 
131
 
132
  # --- Main Execution Logic ---
@@ -159,7 +147,7 @@ if predictor and predictor.model: # Check if predictor initialized successfully
159
 
160
  print("Launching Gradio interface...")
161
  # Set share=True as requested
162
- iface.launch(share=True, debug=True, show_error=True)
163
 
164
  except Exception as e:
165
  print(f"Error creating or launching Gradio interface: {e}")
 
98
  raise RuntimeError("Model loading failed.") from e
99
 
100
  # --- Prediction Method ---
101
+ # Inside the ImagePredictor class:
102
+ def predict(self, image: Image.Image):
103
+ print("--- Predict function called ---") # Check if this even prints in Space logs
104
+ if image is None:
105
+ print("Input image is None")
106
+ return None
107
+ try:
108
+ # Simulate some processing time
109
+ import time
110
+ time.sleep(0.1)
111
+ # Return a dummy dictionary, bypassing all model/transform logic
112
+ dummy_output = {"fake": 0.6, "real": 0.4} # Use your actual labels
113
+ print(f"Returning dummy output: {dummy_output}")
114
+ return dummy_output
115
+ except Exception as e:
116
+ print(f"Error in *simplified* predict: {e}")
117
+ return {"Error": f"Simplified prediction failed: {str(e)}"}
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
 
120
  # --- Main Execution Logic ---
 
147
 
148
  print("Launching Gradio interface...")
149
  # Set share=True as requested
150
+ iface.launch(share=True, debug=True, show_error=True).queue()
151
 
152
  except Exception as e:
153
  print(f"Error creating or launching Gradio interface: {e}")
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
- gradio
2
- torchvision
3
- transformers
4
- tensorflow
5
- torch
 
 
 
1
+ transformers>=4.30.0 # Or a specific recent version
2
+ torch>=2.0.0 # Or a specific recent version compatible with transformers & cuda version in space
3
+ torchvision>=0.15.0 # Or a specific recent version compatible with torch
4
+ gradio>=4.0.0 # Or a specific recent version
5
+ Pillow>=9.0.0
6
+ accelerate>=0.20.0 # Often helpful, especially with safetensors
7
+ numpy