Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import torch
|
| 2 |
from transformers import (
|
| 3 |
Qwen2VLForConditionalGeneration,
|
| 4 |
AutoProcessor,
|
|
@@ -34,34 +33,26 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 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):
|
| 67 |
# First, process with vision model
|
|
@@ -101,10 +92,6 @@ 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,6 +143,7 @@ def process_video_for_code(video_path, max_frames=16, frame_interval=30):
|
|
| 156 |
|
| 157 |
cap.release()
|
| 158 |
|
|
|
|
| 159 |
if frames:
|
| 160 |
return process_image_for_code(frames[0])
|
| 161 |
else:
|
|
@@ -166,16 +154,13 @@ def process_content(content):
|
|
| 166 |
if content is None:
|
| 167 |
return "Please upload an image or video file of code with errors.", ""
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 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,8 +173,8 @@ iface = gr.Interface(
|
|
| 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
|
| 192 |
)
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
from transformers import (
|
| 2 |
Qwen2VLForConditionalGeneration,
|
| 3 |
AutoProcessor,
|
|
|
|
| 33 |
|
| 34 |
vision_model, vision_processor, code_model, code_tokenizer = load_models()
|
| 35 |
|
| 36 |
+
VISION_SYSTEM_PROMPT = """You are an AI assistant specialized in analyzing images and videos of code editors. Your task is to:
|
| 37 |
+
1. Extract and describe any code snippets visible in the image
|
| 38 |
+
2. Identify any error messages, warnings, or highlighting that indicates bugs
|
| 39 |
+
3. Describe the programming language and context if visible
|
| 40 |
+
Be thorough and accurate in your description, as this will be used to fix the code.
|
| 41 |
|
| 42 |
+
Note: If unwanted contents like Harassment or bullying, Hate speech or discriminatory content, Sexually explicit material, Dangerous or harmful content appears,
|
| 43 |
+
please don't process it and 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."
|
| 44 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
CODE_SYSTEM_PROMPT = """You are an expert code debugging assistant. Based on the description of code and errors provided, your task is to:
|
| 47 |
+
1. Identify the bugs and issues in the code
|
| 48 |
+
2. Provide a corrected version of the code
|
| 49 |
+
3. Explain the fixes made and why they resolve the issues
|
| 50 |
+
4. Provide the output in a well-structured format removing all the unnecessary information
|
| 51 |
+
Be thorough in your explanation and ensure the corrected code is complete and functional.
|
| 52 |
|
| 53 |
+
Note: 1. Check if the input description contains any flags for inappropriate content.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
If it does, respond ONLY with: "I apologize, but I cannot process this request as the original content was flagged as inappropriate."
|
| 55 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def process_image_for_code(image):
|
| 58 |
# First, process with vision model
|
|
|
|
| 92 |
clean_up_tokenization_spaces=False
|
| 93 |
)[0]
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Then, use code model to fix the code
|
| 96 |
code_messages = [
|
| 97 |
{"role": "system", "content": CODE_SYSTEM_PROMPT},
|
|
|
|
| 143 |
|
| 144 |
cap.release()
|
| 145 |
|
| 146 |
+
# Process the first frame for now (you could extend this to handle multiple frames)
|
| 147 |
if frames:
|
| 148 |
return process_image_for_code(frames[0])
|
| 149 |
else:
|
|
|
|
| 154 |
if content is None:
|
| 155 |
return "Please upload an image or video file of code with errors.", ""
|
| 156 |
|
| 157 |
+
if content.name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 158 |
+
image = Image.open(content.name)
|
| 159 |
+
vision_output, code_output = process_image_for_code(image)
|
| 160 |
+
elif content.name.lower().endswith(('.mp4', '.avi', '.mov')):
|
| 161 |
+
vision_output, code_output = process_video_for_code(content.name)
|
| 162 |
+
else:
|
| 163 |
+
return "Unsupported file type. Please provide an image or video file.", ""
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
return vision_output, code_output
|
| 166 |
|
|
|
|
| 173 |
gr.Code(label="Fixed Code", language="python")
|
| 174 |
],
|
| 175 |
title="Vision Code Debugger",
|
| 176 |
+
description="Upload an image or video of code with errors, and the AI will analyze and fix the issues."
|
| 177 |
)
|
| 178 |
|
| 179 |
if __name__ == "__main__":
|
| 180 |
+
iface.launch()
|