Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,89 +10,77 @@ client = None
|
|
| 10 |
|
| 11 |
def initialize_client(api_key=None):
|
| 12 |
global client
|
| 13 |
-
# Fetch the API key from the environment if it's not provided directly
|
| 14 |
api_key = os.getenv("TOGETHER_API_KEY")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
def encode_image(image_path):
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
max_new_tokens = 350 # Increase for potentially longer responses
|
| 27 |
-
temperature = 0.7
|
| 28 |
if client is None:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
history.append(("Error initializing client", f"{str(e)}"))
|
| 33 |
-
yield history
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
prompt
|
| 37 |
-
|
| 38 |
-
"""
|
| 39 |
-
|
| 40 |
-
messages = [{"role": "system", "content": prompt}]
|
| 41 |
-
|
| 42 |
-
# Encode the image and add to messages
|
| 43 |
image_base64 = encode_image(image_path)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
model="meta-llama/Llama-Vision-Free",
|
| 58 |
messages=messages,
|
| 59 |
-
max_tokens=
|
| 60 |
-
temperature=
|
| 61 |
-
stream=True,
|
| 62 |
)
|
| 63 |
|
| 64 |
-
response
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if not response.strip():
|
| 73 |
-
history[-1] = ("Image uploaded", "No response generated. Please try again.")
|
| 74 |
-
yield history
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if "Request Entity Too Large" in str(e)
|
| 80 |
-
else f"An error occurred: {str(e)}"
|
| 81 |
-
)
|
| 82 |
-
history[-1] = ("Image uploaded", error_message)
|
| 83 |
-
yield history
|
| 84 |
|
| 85 |
-
#
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Radiator Accident Detection")
|
| 88 |
-
gr.Markdown("Upload an image of a radiator to determine if it shows signs of an accident")
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
demo.launch(
|
|
|
|
| 10 |
|
| 11 |
def initialize_client(api_key=None):
|
| 12 |
global client
|
|
|
|
| 13 |
api_key = os.getenv("TOGETHER_API_KEY")
|
| 14 |
+
if api_key:
|
| 15 |
+
client = Together()
|
| 16 |
+
print("Client initialized successfully.")
|
| 17 |
+
else:
|
| 18 |
+
print("API key not found. Please set TOGETHER_API_KEY environment variable.")
|
| 19 |
|
| 20 |
def encode_image(image_path):
|
| 21 |
+
try:
|
| 22 |
+
with Image.open(image_path) as img:
|
| 23 |
+
buffered = io.BytesIO()
|
| 24 |
+
img.save(buffered, format="PNG")
|
| 25 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Error encoding image: {str(e)}")
|
| 28 |
+
return None
|
| 29 |
|
| 30 |
+
def detect_accident(image_path):
|
|
|
|
|
|
|
| 31 |
if client is None:
|
| 32 |
+
initialize_client()
|
| 33 |
+
if client is None:
|
| 34 |
+
return "Error: Client not initialized."
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# This is a simple prompt to guide the model
|
| 37 |
+
prompt = "Given the image of a radiator, respond with 'accident' if it shows signs of an accident or 'not accident' if it does not."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
image_base64 = encode_image(image_path)
|
| 39 |
+
|
| 40 |
+
if not image_base64:
|
| 41 |
+
return "Error: Could not encode the image. Please try again with a different one."
|
| 42 |
+
|
| 43 |
+
messages = [
|
| 44 |
+
{"role": "system", "content": prompt},
|
| 45 |
+
{
|
| 46 |
+
"role": "user",
|
| 47 |
+
"content": [
|
| 48 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
|
| 49 |
+
],
|
| 50 |
+
},
|
| 51 |
+
]
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
response = client.chat.completions.create(
|
| 55 |
model="meta-llama/Llama-Vision-Free",
|
| 56 |
messages=messages,
|
| 57 |
+
max_tokens=10, # Keep this low to limit the response
|
| 58 |
+
temperature=0.0, # Reduce randomness
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Extract the response and clean it
|
| 62 |
+
response_text = response.choices[0].message.content.strip().lower()
|
| 63 |
+
|
| 64 |
+
# Validate the response
|
| 65 |
+
if response_text == "accident" or response_text == "not accident":
|
| 66 |
+
return response_text
|
| 67 |
+
else:
|
| 68 |
+
return "Error: Unexpected response format."
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
+
print(f"Error during model call: {str(e)}")
|
| 72 |
+
return f"Error: Could not process the image. Details: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Gradio interface
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown("# Radiator Accident Detection")
|
| 77 |
+
gr.Markdown("Upload an image of a radiator to determine if it shows signs of an accident.")
|
| 78 |
|
| 79 |
+
image_input = gr.Image(type="filepath")
|
| 80 |
+
result = gr.Textbox(label="Result")
|
| 81 |
+
submit = gr.Button("Submit")
|
| 82 |
|
| 83 |
+
submit.click(detect_accident, inputs=image_input, outputs=result)
|
|
|
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
demo.launch(debug=True)
|