Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,80 +11,85 @@ client = None
|
|
| 11 |
def initialize_client(api_key=None):
|
| 12 |
global client
|
| 13 |
api_key = os.getenv("TOGETHER_API_KEY")
|
| 14 |
-
|
| 15 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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
|
|
|
|
|
|
|
| 31 |
if client is None:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
prompt = "
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if not image_base64:
|
| 40 |
-
return "Error: Could not encode the image. Please try again with a different one."
|
| 41 |
|
| 42 |
-
messages = [
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
model="meta-llama/Llama-Vision-Free",
|
| 55 |
messages=messages,
|
| 56 |
-
max_tokens=
|
| 57 |
-
temperature=
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# Return the expected results
|
| 67 |
-
if "accident" in response_text:
|
| 68 |
-
return "accident"
|
| 69 |
-
elif "not accident" in response_text:
|
| 70 |
-
return "not accident"
|
| 71 |
-
else:
|
| 72 |
-
return f"Error: Unexpected response format. Received: {response_text}"
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# Gradio interface
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown("# Radiator Accident Detection")
|
| 81 |
-
gr.Markdown("Upload an image of a radiator to determine if it shows signs of an accident
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch(debug=True)
|
|
|
|
| 11 |
def initialize_client(api_key=None):
|
| 12 |
global client
|
| 13 |
api_key = os.getenv("TOGETHER_API_KEY")
|
| 14 |
+
print(api_key)
|
| 15 |
+
client = Together()
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def encode_image(image_path):
|
| 18 |
+
with Image.open(image_path) as img:
|
| 19 |
+
buffered = io.BytesIO()
|
| 20 |
+
img.save(buffered, format="PNG")
|
| 21 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def bot_streaming(image_path, history):
|
| 24 |
+
max_new_tokens = 250
|
| 25 |
+
temperature = 0.7
|
| 26 |
if client is None:
|
| 27 |
+
try:
|
| 28 |
+
initialize_client()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
history.append(("Error initializing client", f"{str(e)}"))
|
| 31 |
+
yield history
|
| 32 |
+
return
|
| 33 |
|
| 34 |
+
prompt = """
|
| 35 |
+
Determine if the radiator in the image shows signs of being involved in an accident or not
|
| 36 |
+
"""
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
messages = [{"role": "system", "content": prompt}]
|
| 39 |
+
|
| 40 |
+
# Encode the image and add to messages
|
| 41 |
+
image_base64 = encode_image(image_path)
|
| 42 |
+
messages.append({
|
| 43 |
+
"role": "user",
|
| 44 |
+
"content": [
|
| 45 |
+
{
|
| 46 |
+
"type": "image_url",
|
| 47 |
+
"image_url": {"url": f"data:image/png;base64,{image_base64}"}
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
})
|
| 51 |
+
history = history + [("Image uploaded", "")]
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
stream = client.chat.completions.create(
|
| 55 |
model="meta-llama/Llama-Vision-Free",
|
| 56 |
messages=messages,
|
| 57 |
+
max_tokens=max_new_tokens,
|
| 58 |
+
temperature=temperature,
|
| 59 |
+
stream=True,
|
| 60 |
)
|
| 61 |
|
| 62 |
+
response = ""
|
| 63 |
+
for chunk in stream:
|
| 64 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None:
|
| 65 |
+
response += chunk.choices[0].delta.content
|
| 66 |
+
history[-1] = ("Image uploaded", response)
|
| 67 |
+
yield history
|
| 68 |
|
| 69 |
+
if not response:
|
| 70 |
+
history[-1] = ("Image uploaded", "No response generated. Please try again.")
|
| 71 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
error_message = (
|
| 75 |
+
"The image is too large. Please try with a smaller image or compress the existing one."
|
| 76 |
+
if "Request Entity Too Large" in str(e)
|
| 77 |
+
else f"An error occurred: {str(e)}"
|
| 78 |
+
)
|
| 79 |
+
history[-1] = ("Image uploaded", error_message)
|
| 80 |
+
yield history
|
| 81 |
|
| 82 |
+
# Set up Gradio interface
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.Markdown("# Radiator Accident Detection")
|
| 85 |
+
gr.Markdown("Upload an image of a radiator to determine if it shows signs of an accident")
|
| 86 |
|
| 87 |
+
chatbot = gr.Chatbot()
|
| 88 |
+
img = gr.Image(type="filepath", label="Upload Radiator Image")
|
| 89 |
+
clear = gr.Button("Clear")
|
| 90 |
|
| 91 |
+
img.upload(bot_streaming, inputs=[img, chatbot], outputs=chatbot)
|
| 92 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 93 |
|
| 94 |
if __name__ == "__main__":
|
| 95 |
demo.launch(debug=True)
|