Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,17 +17,16 @@ OPENROUTER_API_KEY = "sk-or-v1-cf4abd8adde58255d49e31d05fbe3f87d2bbfcdb50eb1dbef
|
|
17 |
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
18 |
MODEL_NAME = "mistralai/mistral-small-24b-instruct-2501:free"
|
19 |
|
20 |
-
# Define input shape
|
21 |
input_shape = (224, 224, 3)
|
22 |
|
23 |
def preprocess_image(image, target_size):
|
24 |
"""Preprocess the input image for the model."""
|
25 |
if image is None:
|
26 |
raise ValueError("No image provided")
|
27 |
-
image = image.convert("RGB")
|
28 |
image = image.resize(target_size)
|
29 |
image_array = np.array(image)
|
30 |
-
image_array = image_array / 255.0
|
31 |
return image_array
|
32 |
|
33 |
def get_medical_guidelines(wound_type):
|
@@ -36,7 +35,7 @@ def get_medical_guidelines(wound_type):
|
|
36 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
37 |
"Content-Type": "application/json",
|
38 |
"HTTP-Referer": "https://huggingface.co/spaces/MahatirTusher/Wound_Treatment",
|
39 |
-
"X-Title": "
|
40 |
}
|
41 |
|
42 |
prompt = f"""As a medical professional, provide detailed guidelines for treating a {wound_type} wound.
|
@@ -63,7 +62,7 @@ def predict(image):
|
|
63 |
input_data = np.expand_dims(input_data, axis=0)
|
64 |
|
65 |
# Load class labels
|
66 |
-
with open('
|
67 |
class_labels = file.read().splitlines()
|
68 |
|
69 |
if len(class_labels) != model.output_shape[-1]:
|
@@ -72,15 +71,15 @@ def predict(image):
|
|
72 |
# Make prediction
|
73 |
predictions = model.predict(input_data)
|
74 |
results = {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}
|
75 |
-
predicted_class =
|
76 |
|
77 |
# Get medical guidelines
|
78 |
guidelines = get_medical_guidelines(predicted_class)
|
79 |
|
80 |
-
return
|
81 |
|
82 |
except Exception as e:
|
83 |
-
return {"
|
84 |
|
85 |
# Gradio Interface
|
86 |
iface = gr.Interface(
|
|
|
17 |
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
18 |
MODEL_NAME = "mistralai/mistral-small-24b-instruct-2501:free"
|
19 |
|
|
|
20 |
input_shape = (224, 224, 3)
|
21 |
|
22 |
def preprocess_image(image, target_size):
|
23 |
"""Preprocess the input image for the model."""
|
24 |
if image is None:
|
25 |
raise ValueError("No image provided")
|
26 |
+
image = image.convert("RGB")
|
27 |
image = image.resize(target_size)
|
28 |
image_array = np.array(image)
|
29 |
+
image_array = image_array / 255.0
|
30 |
return image_array
|
31 |
|
32 |
def get_medical_guidelines(wound_type):
|
|
|
35 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
36 |
"Content-Type": "application/json",
|
37 |
"HTTP-Referer": "https://huggingface.co/spaces/MahatirTusher/Wound_Treatment",
|
38 |
+
"X-Title": "Wound Treatment Advisor"
|
39 |
}
|
40 |
|
41 |
prompt = f"""As a medical professional, provide detailed guidelines for treating a {wound_type} wound.
|
|
|
62 |
input_data = np.expand_dims(input_data, axis=0)
|
63 |
|
64 |
# Load class labels
|
65 |
+
with open('classes.txt', 'r') as file:
|
66 |
class_labels = file.read().splitlines()
|
67 |
|
68 |
if len(class_labels) != model.output_shape[-1]:
|
|
|
71 |
# Make prediction
|
72 |
predictions = model.predict(input_data)
|
73 |
results = {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}
|
74 |
+
predicted_class = max(results, key=results.get)
|
75 |
|
76 |
# Get medical guidelines
|
77 |
guidelines = get_medical_guidelines(predicted_class)
|
78 |
|
79 |
+
return results, guidelines # Return as tuple instead of dict
|
80 |
|
81 |
except Exception as e:
|
82 |
+
return {f"Error: {str(e)}"}, ""
|
83 |
|
84 |
# Gradio Interface
|
85 |
iface = gr.Interface(
|