whyumesh commited on
Commit
b49d6ac
·
verified ·
1 Parent(s): 74a6b46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -20
app.py CHANGED
@@ -34,17 +34,33 @@ def load_models():
34
 
35
  vision_model, vision_processor, code_model, code_tokenizer = load_models()
36
 
37
- VISION_SYSTEM_PROMPT = """You are an AI assistant specialized in analyzing images and videos of code editors. Your task is to:
38
- 1. Extract and describe any code snippets visible in the image
39
- 2. Identify any error messages, warnings, or highlighting that indicates bugs
40
- 3. Describe the programming language and context if visible
41
- Be thorough and accurate in your description, as this will be used to fix the code."""
42
-
43
- CODE_SYSTEM_PROMPT = """You are an expert code debugging assistant. Based on the description of code and errors provided, your task is to:
44
- 1. Identify the bugs and issues in the code
45
- 2. Provide a corrected version of the code
46
- 3. Explain the fixes made and why they resolve the issues
47
- 4. Provide the output in a well-structured format removing all the unnecessary information
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  Be thorough in your explanation and ensure the corrected code is complete and functional."""
49
 
50
  def process_image_for_code(image):
@@ -85,6 +101,10 @@ def process_image_for_code(image):
85
  clean_up_tokenization_spaces=False
86
  )[0]
87
 
 
 
 
 
88
  # Then, use code model to fix the code
89
  code_messages = [
90
  {"role": "system", "content": CODE_SYSTEM_PROMPT},
@@ -136,7 +156,6 @@ def process_video_for_code(video_path, max_frames=16, frame_interval=30):
136
 
137
  cap.release()
138
 
139
- # Process the first frame for now (you could extend this to handle multiple frames)
140
  if frames:
141
  return process_image_for_code(frames[0])
142
  else:
@@ -147,13 +166,16 @@ def process_content(content):
147
  if content is None:
148
  return "Please upload an image or video file of code with errors.", ""
149
 
150
- if content.name.lower().endswith(('.png', '.jpg', '.jpeg')):
151
- image = Image.open(content.name)
152
- vision_output, code_output = process_image_for_code(image)
153
- elif content.name.lower().endswith(('.mp4', '.avi', '.mov')):
154
- vision_output, code_output = process_video_for_code(content.name)
155
- else:
156
- return "Unsupported file type. Please provide an image or video file.", ""
 
 
 
157
 
158
  return vision_output, code_output
159
 
@@ -166,7 +188,7 @@ iface = gr.Interface(
166
  gr.Code(label="Fixed Code", language="python")
167
  ],
168
  title="Vision Code Debugger",
169
- description="Upload an image or video of code with errors, and the AI will analyze and fix the issues."
170
  )
171
 
172
  if __name__ == "__main__":
 
34
 
35
  vision_model, vision_processor, code_model, code_tokenizer = load_models()
36
 
37
+ VISION_SYSTEM_PROMPT = """You are an AI assistant specialized in analyzing images and videos of code editors. Your primary task is to:
38
+
39
+ 1. FIRST AND MOST IMPORTANTLY: Check if the image contains any inappropriate content such as:
40
+ - Harassment or bullying
41
+ - Hate speech or discriminatory content
42
+ - Sexually explicit material
43
+ - Dangerous or harmful content
44
+ If any such content is detected, respond ONLY with: "I apologize, but I cannot process this content as it appears to contain [type of inappropriate content]. Please provide only appropriate code-related images."
45
+
46
+ 2. If the content is appropriate, then:
47
+ - Extract and describe any code snippets visible in the image
48
+ - Identify any error messages, warnings, or highlighting that indicates bugs
49
+ - Describe the programming language and context if visible
50
+
51
+ Be thorough and accurate in your description of appropriate content, as this will be used to fix the code."""
52
+
53
+ CODE_SYSTEM_PROMPT = """You are an expert code debugging assistant. Your tasks in order are:
54
+
55
+ 1. Check if the input description contains any flags for inappropriate content.
56
+ If it does, respond ONLY with: "I apologize, but I cannot process this request as the original content was flagged as inappropriate."
57
+
58
+ 2. If the content is appropriate, then based on the description of code and errors provided:
59
+ - Identify the bugs and issues in the code
60
+ - Provide a corrected version of the code
61
+ - Explain the fixes made and why they resolve the issues
62
+ - Provide the output in a well-structured format removing all unnecessary information
63
+
64
  Be thorough in your explanation and ensure the corrected code is complete and functional."""
65
 
66
  def process_image_for_code(image):
 
101
  clean_up_tokenization_spaces=False
102
  )[0]
103
 
104
+ # Check if vision model flagged inappropriate content
105
+ if "I apologize, but I cannot process this content" in vision_description:
106
+ return vision_description, "No code analysis provided due to inappropriate content."
107
+
108
  # Then, use code model to fix the code
109
  code_messages = [
110
  {"role": "system", "content": CODE_SYSTEM_PROMPT},
 
156
 
157
  cap.release()
158
 
 
159
  if frames:
160
  return process_image_for_code(frames[0])
161
  else:
 
166
  if content is None:
167
  return "Please upload an image or video file of code with errors.", ""
168
 
169
+ try:
170
+ if content.name.lower().endswith(('.png', '.jpg', '.jpeg')):
171
+ image = Image.open(content.name)
172
+ vision_output, code_output = process_image_for_code(image)
173
+ elif content.name.lower().endswith(('.mp4', '.avi', '.mov')):
174
+ vision_output, code_output = process_video_for_code(content.name)
175
+ else:
176
+ return "Unsupported file type. Please provide an image or video file.", ""
177
+ except Exception as e:
178
+ return f"An error occurred while processing the file: {str(e)}", ""
179
 
180
  return vision_output, code_output
181
 
 
188
  gr.Code(label="Fixed Code", language="python")
189
  ],
190
  title="Vision Code Debugger",
191
+ description="Upload an image or video of code with errors for AI analysis and fixes. Note: Only appropriate code-related content will be processed."
192
  )
193
 
194
  if __name__ == "__main__":