Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ from dotenv import load_dotenv
|
|
| 5 |
import nltk
|
| 6 |
from nltk.tokenize import sent_tokenize
|
| 7 |
import pandas as pd
|
| 8 |
-
import json
|
| 9 |
|
| 10 |
# Initialize NLTK
|
| 11 |
nltk.download('punkt', quiet=True)
|
|
@@ -29,7 +28,6 @@ class CodeCopilot:
|
|
| 29 |
}
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
# Correct API endpoint based on Blackbox documentation
|
| 33 |
response = requests.post(
|
| 34 |
"https://api.blackbox.ai/chat/completions",
|
| 35 |
headers=headers,
|
|
@@ -48,16 +46,31 @@ class CodeCopilot:
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"Processing Error: {str(e)}"
|
| 50 |
|
|
|
|
|
|
|
| 51 |
def analyze_code_patterns(self, text):
|
| 52 |
-
"""Analyze text for coding patterns"""
|
| 53 |
-
sentences = sent_tokenize(text)
|
| 54 |
patterns = {
|
| 55 |
-
'function_def':
|
| 56 |
-
'class_def':
|
| 57 |
-
'loop':
|
| 58 |
-
'conditional':
|
| 59 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return patterns
|
|
|
|
| 61 |
|
| 62 |
def generate_suggestions(self, patterns):
|
| 63 |
"""Generate suggestions based on detected patterns"""
|
|
@@ -72,10 +85,8 @@ class CodeCopilot:
|
|
| 72 |
|
| 73 |
def process_input(self, user_input):
|
| 74 |
"""Process user input and generate response"""
|
| 75 |
-
# Analyze patterns
|
| 76 |
patterns = self.analyze_code_patterns(user_input)
|
| 77 |
|
| 78 |
-
# Create context-aware prompt
|
| 79 |
context = "\nPrevious conversation:\n" + "\n".join(
|
| 80 |
[f"User: {h[0]}\nAI: {h[1]}" for h in self.chat_history[-self.context_window:]])
|
| 81 |
|
|
@@ -87,11 +98,9 @@ class CodeCopilot:
|
|
| 87 |
{user_input}
|
| 88 |
"""
|
| 89 |
|
| 90 |
-
# Get response
|
| 91 |
response = self.get_blackbox_response(prompt)
|
| 92 |
suggestions = self.generate_suggestions(patterns)
|
| 93 |
|
| 94 |
-
# Update chat history
|
| 95 |
self.chat_history.append((user_input, response))
|
| 96 |
|
| 97 |
return response, patterns, suggestions
|
|
@@ -100,30 +109,45 @@ class CodeCopilot:
|
|
| 100 |
copilot = CodeCopilot()
|
| 101 |
|
| 102 |
# Gradio interface
|
| 103 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="AI Code Copilot") as demo:
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
-
with gr.Column(scale=3):
|
| 108 |
input_text = gr.Textbox(
|
| 109 |
-
label="
|
| 110 |
-
placeholder="Paste
|
| 111 |
-
lines=
|
|
|
|
| 112 |
)
|
| 113 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
| 114 |
-
|
| 115 |
-
with gr.Column(scale=
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
def process_input(user_input):
|
| 128 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 129 |
pattern_df = pd.DataFrame({
|
|
@@ -137,7 +161,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Code Copilot") as demo:
|
|
| 137 |
inputs=input_text,
|
| 138 |
outputs=[output_text, suggestions, pattern_display]
|
| 139 |
)
|
| 140 |
-
|
| 141 |
input_text.submit(
|
| 142 |
fn=process_input,
|
| 143 |
inputs=input_text,
|
|
|
|
| 5 |
import nltk
|
| 6 |
from nltk.tokenize import sent_tokenize
|
| 7 |
import pandas as pd
|
|
|
|
| 8 |
|
| 9 |
# Initialize NLTK
|
| 10 |
nltk.download('punkt', quiet=True)
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
response = requests.post(
|
| 32 |
"https://api.blackbox.ai/chat/completions",
|
| 33 |
headers=headers,
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Processing Error: {str(e)}"
|
| 48 |
|
| 49 |
+
import ast
|
| 50 |
+
|
| 51 |
def analyze_code_patterns(self, text):
|
|
|
|
|
|
|
| 52 |
patterns = {
|
| 53 |
+
'function_def': 0,
|
| 54 |
+
'class_def': 0,
|
| 55 |
+
'loop': 0,
|
| 56 |
+
'conditional': 0
|
| 57 |
}
|
| 58 |
+
try:
|
| 59 |
+
tree = ast.parse(text)
|
| 60 |
+
for node in ast.walk(tree):
|
| 61 |
+
if isinstance(node, ast.FunctionDef):
|
| 62 |
+
patterns['function_def'] += 1
|
| 63 |
+
elif isinstance(node, ast.ClassDef):
|
| 64 |
+
patterns['class_def'] += 1
|
| 65 |
+
elif isinstance(node, (ast.For, ast.While)):
|
| 66 |
+
patterns['loop'] += 1
|
| 67 |
+
elif isinstance(node, ast.If):
|
| 68 |
+
patterns['conditional'] += 1
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"AST parse error: {e}")
|
| 71 |
+
|
| 72 |
return patterns
|
| 73 |
+
|
| 74 |
|
| 75 |
def generate_suggestions(self, patterns):
|
| 76 |
"""Generate suggestions based on detected patterns"""
|
|
|
|
| 85 |
|
| 86 |
def process_input(self, user_input):
|
| 87 |
"""Process user input and generate response"""
|
|
|
|
| 88 |
patterns = self.analyze_code_patterns(user_input)
|
| 89 |
|
|
|
|
| 90 |
context = "\nPrevious conversation:\n" + "\n".join(
|
| 91 |
[f"User: {h[0]}\nAI: {h[1]}" for h in self.chat_history[-self.context_window:]])
|
| 92 |
|
|
|
|
| 98 |
{user_input}
|
| 99 |
"""
|
| 100 |
|
|
|
|
| 101 |
response = self.get_blackbox_response(prompt)
|
| 102 |
suggestions = self.generate_suggestions(patterns)
|
| 103 |
|
|
|
|
| 104 |
self.chat_history.append((user_input, response))
|
| 105 |
|
| 106 |
return response, patterns, suggestions
|
|
|
|
| 109 |
copilot = CodeCopilot()
|
| 110 |
|
| 111 |
# Gradio interface
|
| 112 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="🤖 AI Code Copilot") as demo:
|
| 113 |
+
# Header
|
| 114 |
+
gr.Markdown(
|
| 115 |
+
"""
|
| 116 |
+
<div style='text-align: center; margin-bottom: 1rem;'>
|
| 117 |
+
<h1>🤖 AI Code Copilot</h1>
|
| 118 |
+
<p>Your interactive assistant for code suggestions and analysis</p>
|
| 119 |
+
</div>
|
| 120 |
+
"""
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Main layout: input on the left, outputs on the right
|
| 124 |
with gr.Row():
|
| 125 |
+
with gr.Column(scale=3, min_width=300):
|
| 126 |
input_text = gr.Textbox(
|
| 127 |
+
label="Enter your code or question",
|
| 128 |
+
placeholder="Paste code snippets or ask a coding question...",
|
| 129 |
+
lines=10,
|
| 130 |
+
interactive=True
|
| 131 |
)
|
| 132 |
+
submit_btn = gr.Button("🚀 Generate", variant="primary")
|
| 133 |
+
|
| 134 |
+
with gr.Column(scale=5, min_width=500):
|
| 135 |
+
# Display response
|
| 136 |
+
gr.Markdown("**Assistant Response:**")
|
| 137 |
+
output_text = gr.Markdown()
|
| 138 |
+
# Display suggestions
|
| 139 |
+
gr.Markdown("**Suggestions:**")
|
| 140 |
+
suggestions = gr.Markdown()
|
| 141 |
+
# Display pattern analysis
|
| 142 |
+
gr.Markdown("**Pattern Analysis:**")
|
| 143 |
+
pattern_display = gr.Dataframe(
|
| 144 |
+
headers=["Pattern", "Count"],
|
| 145 |
+
datatype=["str", "number"],
|
| 146 |
+
interactive=False,
|
| 147 |
+
label="Detected code patterns"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Processing function for both button and enter
|
| 151 |
def process_input(user_input):
|
| 152 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 153 |
pattern_df = pd.DataFrame({
|
|
|
|
| 161 |
inputs=input_text,
|
| 162 |
outputs=[output_text, suggestions, pattern_display]
|
| 163 |
)
|
|
|
|
| 164 |
input_text.submit(
|
| 165 |
fn=process_input,
|
| 166 |
inputs=input_text,
|