arshadrana commited on
Commit
ca2909c
·
verified ·
1 Parent(s): 30e081d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -11,21 +11,34 @@ client = None
11
  def initialize_client(api_key=None):
12
  global client
13
  api_key = os.getenv("TOGETHER_API_KEY")
14
- client = Together()
 
 
 
 
15
 
16
  def encode_image(image_path):
17
- with Image.open(image_path) as img:
18
- buffered = io.BytesIO()
19
- img.save(buffered, format="PNG")
20
- return base64.b64encode(buffered.getvalue()).decode("utf-8")
 
 
 
 
21
 
22
  def detect_accident(image_path):
23
  if client is None:
24
  initialize_client()
 
 
25
 
26
  prompt = "Determine if the radiator in the image shows signs of being involved in an accident or not."
27
  image_base64 = encode_image(image_path)
28
 
 
 
 
29
  messages = [
30
  {"role": "system", "content": prompt},
31
  {
@@ -43,12 +56,13 @@ def detect_accident(image_path):
43
  max_tokens=10,
44
  temperature=0.5
45
  ).choices[0].message["content"].strip()
46
-
47
- # Limit the response to a simple "accident" or "not accident" output
48
  return "accident" if "accident" in response.lower() else "not accident"
49
 
50
  except Exception as e:
51
- return "Error: Could not process the image. Please try again with a different one."
 
52
 
53
  # Gradio interface
54
  with gr.Blocks() as demo:
 
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 = "Determine if the radiator in the image shows signs of being involved in an accident or not."
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
  {
 
56
  max_tokens=10,
57
  temperature=0.5
58
  ).choices[0].message["content"].strip()
59
+
60
+ # Check if response contains "accident" or "not accident"
61
  return "accident" if "accident" in response.lower() else "not accident"
62
 
63
  except Exception as e:
64
+ print(f"Error during model call: {str(e)}")
65
+ return f"Error: Could not process the image. Please try again with a different one."
66
 
67
  # Gradio interface
68
  with gr.Blocks() as demo: