Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
import json
|
| 4 |
+
import tempfile
|
| 5 |
+
|
| 6 |
+
# Initialize both external Spaces
|
| 7 |
+
client1 = Client("raqiat123/crop_disease_detection")
|
| 8 |
+
client2 = Client("SoraRyuu/cv_first")
|
| 9 |
+
|
| 10 |
+
def combined_predict(image):
|
| 11 |
+
# Temporarily save uploaded file
|
| 12 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 13 |
+
image.save(tmp.name)
|
| 14 |
+
tmp_path = tmp.name
|
| 15 |
+
|
| 16 |
+
# Run prediction on both models
|
| 17 |
+
result1 = client1.predict(image=handle_file(tmp_path), api_name="/predict")
|
| 18 |
+
result2 = client2.predict(image=handle_file(tmp_path), api_name="/predict")
|
| 19 |
+
|
| 20 |
+
# Extract confidence values (adjust if needed)
|
| 21 |
+
conf1 = result1.get("confidence") if isinstance(result1, dict) else 0
|
| 22 |
+
conf2 = result2.get("confidence") if isinstance(result2, dict) else 0
|
| 23 |
+
|
| 24 |
+
if conf1 >= conf2:
|
| 25 |
+
chosen = {
|
| 26 |
+
"chosen_model": "raqiat123/crop_disease_detection",
|
| 27 |
+
"confidence": conf1,
|
| 28 |
+
"output": result1
|
| 29 |
+
}
|
| 30 |
+
result_text = f"Model Selected: crop_disease_detection\nConfidence: {conf1}"
|
| 31 |
+
else:
|
| 32 |
+
chosen = {
|
| 33 |
+
"chosen_model": "SoraRyuu/cv_first",
|
| 34 |
+
"confidence": conf2,
|
| 35 |
+
"output": result2
|
| 36 |
+
}
|
| 37 |
+
result_text = f"Model Selected: cv_first\nConfidence: {conf2}"
|
| 38 |
+
|
| 39 |
+
return result_text, json.dumps(chosen, indent=4)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Build Gradio UI
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# 🌱 Crop Classifier")
|
| 45 |
+
gr.Markdown("Upload an image. The Space will return the disease detected.")
|
| 46 |
+
|
| 47 |
+
image_input = gr.Image(type="pil")
|
| 48 |
+
text_output = gr.Textbox(label="Result (Text)")
|
| 49 |
+
json_output = gr.JSON(label="Raw JSON Result")
|
| 50 |
+
|
| 51 |
+
btn = gr.Button("Run Prediction")
|
| 52 |
+
|
| 53 |
+
btn.click(
|
| 54 |
+
fn=combined_predict,
|
| 55 |
+
inputs=image_input,
|
| 56 |
+
outputs=[text_output, json_output]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|