Spaces:
Sleeping
Sleeping
changes
#1
by
iammahmads
- opened
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
BLACKBOX_API_KEY=
|
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from dotenv import load_dotenv
|
|
| 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,6 +29,7 @@ class CodeCopilot:
|
|
| 28 |
}
|
| 29 |
|
| 30 |
try:
|
|
|
|
| 31 |
response = requests.post(
|
| 32 |
"https://api.blackbox.ai/chat/completions",
|
| 33 |
headers=headers,
|
|
@@ -60,9 +62,9 @@ class CodeCopilot:
|
|
| 60 |
def generate_suggestions(self, patterns):
|
| 61 |
"""Generate suggestions based on detected patterns"""
|
| 62 |
suggestions = []
|
| 63 |
-
if patterns['function_def']
|
| 64 |
suggestions.append("π Consider breaking down into smaller functions or using a class structure.")
|
| 65 |
-
if patterns['loop']
|
| 66 |
suggestions.append("π You might benefit from list comprehensions or map/filter functions.")
|
| 67 |
if patterns['conditional'] > 3:
|
| 68 |
suggestions.append("β Complex conditionals might be simplified using polymorphism or strategy pattern.")
|
|
@@ -70,8 +72,10 @@ class CodeCopilot:
|
|
| 70 |
|
| 71 |
def process_input(self, user_input):
|
| 72 |
"""Process user input and generate response"""
|
|
|
|
| 73 |
patterns = self.analyze_code_patterns(user_input)
|
| 74 |
|
|
|
|
| 75 |
context = "\nPrevious conversation:\n" + "\n".join(
|
| 76 |
[f"User: {h[0]}\nAI: {h[1]}" for h in self.chat_history[-self.context_window:]])
|
| 77 |
|
|
@@ -83,9 +87,11 @@ class CodeCopilot:
|
|
| 83 |
{user_input}
|
| 84 |
"""
|
| 85 |
|
|
|
|
| 86 |
response = self.get_blackbox_response(prompt)
|
| 87 |
suggestions = self.generate_suggestions(patterns)
|
| 88 |
|
|
|
|
| 89 |
self.chat_history.append((user_input, response))
|
| 90 |
|
| 91 |
return response, patterns, suggestions
|
|
@@ -94,46 +100,31 @@ class CodeCopilot:
|
|
| 94 |
copilot = CodeCopilot()
|
| 95 |
|
| 96 |
# Gradio interface
|
| 97 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
"""
|
| 101 |
-
<div style='text-align: center; margin-bottom: 1rem;'>
|
| 102 |
-
<h1>π€ AI Code Copilot</h1>
|
| 103 |
-
<p>Your interactive assistant for code suggestions and analysis</p>
|
| 104 |
-
</div>
|
| 105 |
-
"""
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
# Main layout: input on the left, outputs on the right
|
| 109 |
with gr.Row():
|
| 110 |
-
with gr.Column(scale=3
|
| 111 |
input_text = gr.Textbox(
|
| 112 |
-
label="
|
| 113 |
-
placeholder="Paste code
|
| 114 |
-
lines=
|
| 115 |
-
interactive=True
|
| 116 |
)
|
| 117 |
-
submit_btn = gr.Button("
|
| 118 |
-
|
| 119 |
-
with gr.Column(scale=
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
gr.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
label="Detected code patterns"
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
# Processing function for both button and enter
|
| 136 |
-
def process_input_wrapper(user_input):
|
| 137 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 138 |
pattern_df = pd.DataFrame({
|
| 139 |
"Pattern": list(patterns.keys()),
|
|
@@ -142,14 +133,15 @@ with gr.Blocks(theme=gr.themes.Soft(), title="π€ AI Code Copilot") as demo:
|
|
| 142 |
return response, sugg, pattern_df
|
| 143 |
|
| 144 |
submit_btn.click(
|
| 145 |
-
fn=
|
| 146 |
inputs=input_text,
|
| 147 |
-
outputs=[output_text,
|
| 148 |
)
|
|
|
|
| 149 |
input_text.submit(
|
| 150 |
-
fn=
|
| 151 |
inputs=input_text,
|
| 152 |
-
outputs=[output_text,
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
|
|
|
| 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 |
}
|
| 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,
|
|
|
|
| 62 |
def generate_suggestions(self, patterns):
|
| 63 |
"""Generate suggestions based on detected patterns"""
|
| 64 |
suggestions = []
|
| 65 |
+
if patterns['function_def'] > 3:
|
| 66 |
suggestions.append("π Consider breaking down into smaller functions or using a class structure.")
|
| 67 |
+
if patterns['loop'] > 2:
|
| 68 |
suggestions.append("π You might benefit from list comprehensions or map/filter functions.")
|
| 69 |
if patterns['conditional'] > 3:
|
| 70 |
suggestions.append("β Complex conditionals might be simplified using polymorphism or strategy pattern.")
|
|
|
|
| 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 |
{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 |
copilot = CodeCopilot()
|
| 101 |
|
| 102 |
# Gradio interface
|
| 103 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Code Copilot") as demo:
|
| 104 |
+
gr.Markdown("""<h1 style="text-align: center">π€ AI Code Copilot</h1>""")
|
| 105 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
with gr.Row():
|
| 107 |
+
with gr.Column(scale=3):
|
| 108 |
input_text = gr.Textbox(
|
| 109 |
+
label="Your Code or Question",
|
| 110 |
+
placeholder="Paste your code or ask a question...",
|
| 111 |
+
lines=7
|
|
|
|
| 112 |
)
|
| 113 |
+
submit_btn = gr.Button("Generate", variant="primary")
|
| 114 |
+
|
| 115 |
+
with gr.Column(scale=7):
|
| 116 |
+
with gr.Tab("Assistant Response"):
|
| 117 |
+
output_text = gr.Markdown()
|
| 118 |
+
with gr.Tab("Suggestions"):
|
| 119 |
+
suggestions = gr.Markdown()
|
| 120 |
+
with gr.Tab("Pattern Analysis"):
|
| 121 |
+
pattern_display = gr.Dataframe(
|
| 122 |
+
headers=["Pattern", "Count"],
|
| 123 |
+
datatype=["str", "number"],
|
| 124 |
+
interactive=False
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
def process_input(user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
response, patterns, sugg = copilot.process_input(user_input)
|
| 129 |
pattern_df = pd.DataFrame({
|
| 130 |
"Pattern": list(patterns.keys()),
|
|
|
|
| 133 |
return response, sugg, pattern_df
|
| 134 |
|
| 135 |
submit_btn.click(
|
| 136 |
+
fn=process_input,
|
| 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,
|
| 144 |
+
outputs=[output_text, suggestions, pattern_display]
|
| 145 |
)
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|