arshadrana commited on
Commit
95f5bc6
·
verified ·
1 Parent(s): 89169e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -55
app.py CHANGED
@@ -10,80 +10,89 @@ client = None
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
- prompt = "Does the radiator image show signs of an accident? Answer with 'accident' or 'not accident'."
37
- image_base64 = encode_image(image_path)
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
- {"role": "system", "content": prompt},
44
- {
45
- "role": "user",
46
- "content": [
47
- {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
48
- ],
49
- },
50
- ]
 
 
 
 
 
51
 
52
  try:
53
- # Call the model and print out the response structure for debugging
54
- response = client.chat.completions.create(
55
  model="meta-llama/Llama-Vision-Free",
56
  messages=messages,
57
- max_tokens=10,
58
- temperature=0.5
 
59
  )
60
-
61
- # Print response for debugging
62
- print("API response:", response)
63
 
64
- # Extract text based on observed response format
65
- response_text = response.choices[0].message.content.strip().lower()
66
-
67
- # Only return "accident" or "not accident"
68
- if "accident" in response_text:
69
- return "accident"
70
- else:
71
- return "not accident"
 
 
 
72
 
73
  except Exception as e:
74
- print(f"Error during model call: {str(e)}")
75
- return f"Error: Could not process the image. Details: {str(e)}"
 
 
 
 
 
76
 
77
- # Gradio interface
78
  with gr.Blocks() as demo:
79
  gr.Markdown("# Radiator Accident Detection")
80
- gr.Markdown("Upload an image of a radiator to determine if it shows signs of an accident.")
81
 
82
- image_input = gr.Image(type="filepath")
83
- result = gr.Textbox(label="Result")
84
- submit = gr.Button("Submit")
85
 
86
- submit.click(detect_accident, inputs=image_input, outputs=result)
 
87
 
88
  if __name__ == "__main__":
89
- demo.launch(debug=True)
 
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
+ print(api_key)
16
+
17
+ client = Together()
 
 
18
 
19
  def encode_image(image_path):
20
+ with Image.open(image_path) as img:
21
+ buffered = io.BytesIO()
22
+ img.save(buffered, format="PNG")
23
+ return base64.b64encode(buffered.getvalue()).decode("utf-8")
 
 
 
 
24
 
25
+ def bot_streaming(image_path, history):
26
+ max_new_tokens = 350 # Increase for potentially longer responses
27
+ temperature = 0.7
28
  if client is None:
29
+ try:
30
+ initialize_client()
31
+ except Exception as e:
32
+ history.append(("Error initializing client", f"{str(e)}"))
33
+ yield history
34
+ return
35
 
36
+ prompt = """
37
+ Determine if the radiator in the image shows signs of being involved in an accident or not
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
+ messages.append({
45
+ "role": "user",
46
+ "content": [
47
+ {
48
+ "type": "image_url",
49
+ "image_url": {"url": f"data:image/png;base64,{image_base64}"}
50
+ }
51
+ ]
52
+ })
53
+ history = history + [("Image uploaded", "")]
54
 
55
  try:
56
+ stream = client.chat.completions.create(
 
57
  model="meta-llama/Llama-Vision-Free",
58
  messages=messages,
59
+ max_tokens=max_new_tokens,
60
+ temperature=temperature,
61
+ stream=True,
62
  )
 
 
 
63
 
64
+ response = ""
65
+ for chunk in stream:
66
+ if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content is not None:
67
+ response += chunk.choices[0].delta.content
68
+ history[-1] = ("Image uploaded", response)
69
+ yield history
70
+
71
+ # Handle incomplete responses
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
+ error_message = (
78
+ "The image is too large. Please try with a smaller image or compress the existing one."
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
+ # Set up Gradio interface
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
+ chatbot = gr.Chatbot()
91
+ img = gr.Image(type="filepath", label="Upload Radiator Image")
92
+ clear = gr.Button("Clear")
93
 
94
+ img.upload(bot_streaming, inputs=[img, chatbot], outputs=chatbot)
95
+ clear.click(lambda: None, None, chatbot, queue=False)
96
 
97
  if __name__ == "__main__":
98
+ demo.launch(share=True, debug=True) # Set share=True to create a public link