File size: 5,145 Bytes
dfdbcb5
f29dc1a
1f0a7ed
f29dc1a
1f0a7ed
 
 
 
 
 
 
 
dfdbcb5
f29dc1a
 
1f0a7ed
dfdbcb5
1f0a7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
 
 
 
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
 
 
 
 
f29dc1a
1f0a7ed
 
 
 
 
f947381
1f0a7ed
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f947381
1f0a7ed
 
dfdbcb5
1f0a7ed
 
 
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
 
 
 
 
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
f947381
1f0a7ed
 
 
 
 
 
 
 
f947381
1f0a7ed
 
 
 
f947381
1f0a7ed
 
dfdbcb5
 
1f0a7ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import gradio as gr
import os
import requests
from dotenv import load_dotenv
import nltk
from nltk.tokenize import sent_tokenize
import numpy as np
import pandas as pd
from tqdm import tqdm

# Initialize NLTK (download punkt if needed)
nltk.download('punkt', quiet=True)

# Load environment variables
load_dotenv()
BLACKBOX_API_KEY = os.getenv("BLACKBOX_API_KEY")

class CodeCopilot:
    def __init__(self):
        self.chat_history = []
        self.context_window = 5
        
    def get_blackbox_response(self, prompt, max_tokens=300, temperature=0.7):
        """Get response from Blackbox AI API"""
        headers = {
            "Authorization": f"Bearer {BLACKBOX_API_KEY}",
            "Content-Type": "application/json"
        }
        data = {
            "prompt": prompt,
            "max_tokens": max_tokens,
            "temperature": temperature
        }
        
        try:
            response = requests.post(
                "https://api.blackbox.ai/generate",
                headers=headers,
                json=data,
                timeout=30
            )
            response.raise_for_status()
            return response.json().get("text", "No response text found.")
        except Exception as e:
            return f"Error: {str(e)}"
    
    def analyze_code_patterns(self, text):
        """Analyze text for coding patterns"""
        sentences = sent_tokenize(text)
        # Simple pattern detection (can be expanded)
        patterns = {
            'function_def': sum(1 for s in sentences if 'def ' in s),
            'class_def': sum(1 for s in sentences if 'class ' in s),
            'loop': sum(1 for s in sentences if any(
                word in s for word in ['for ', 'while ', 'loop'])),
            'conditional': sum(1 for s in sentences if any(
                word in s for word in ['if ', 'else ', 'elif ']))
        }
        return patterns
    
    def generate_suggestions(self, patterns):
        """Generate suggestions based on detected patterns"""
        suggestions = []
        if patterns['function_def'] > 3:
            suggestions.append("Consider breaking down into smaller functions or using a class structure.")
        if patterns['loop'] > 2:
            suggestions.append("You might benefit from using list comprehensions or map/filter functions.")
        if patterns['conditional'] > 3:
            suggestions.append("Complex conditionals might be simplified using polymorphism or strategy pattern.")
        return "\n".join(suggestions) if suggestions else "No specific suggestions at this time."
    
    def process_input(self, user_input, history=None):
        """Process user input and generate response"""
        # Analyze patterns
        patterns = self.analyze_code_patterns(user_input)
        suggestions = self.generate_suggestions(patterns)
        
        # Create context-aware prompt
        context = "\n".join([f"User: {h[0]}\nAI: {h[1]}" for h in self.chat_history[-self.context_window:]])
        prompt = f"""
        Context:
        {context}
        
        User Input:
        {user_input}
        
        Respond as a helpful coding assistant that also provides suggestions for improvement.
        Suggestions to consider:
        {suggestions}
        """
        
        # Get response from Blackbox AI
        response = self.get_blackbox_response(prompt)
        
        # Update chat history
        self.chat_history.append((user_input, response))
        
        # Format full response with suggestions
        full_response = f"{response}\n\n=== Suggestions ===\n{suggestions}"
        return full_response

# Initialize copilot
copilot = CodeCopilot()

# Gradio interface
with gr.Blocks(title="AI Code Copilot") as demo:
    gr.Markdown("""
    # AI Code Copilot
    Your intelligent assistant for coding tasks that learns patterns and provides proactive suggestions.
    """)
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                label="Your code or question",
                placeholder="Paste your code or ask a question...",
                lines=5
            )
            submit_btn = gr.Button("Generate")
            
        with gr.Column():
            output_text = gr.Textbox(
                label="Copilot Response",
                lines=10,
                interactive=False
            )
    
    with gr.Accordion("Pattern Analysis", open=False):
        pattern_display = gr.Dataframe(
            headers=["Pattern", "Count"],
            datatype=["str", "number"],
            interactive=False
        )
    
    def process_and_analyze(input_text):
        response = copilot.process_input(input_text)
        patterns = copilot.analyze_code_patterns(input_text)
        pattern_df = pd.DataFrame({
            "Pattern": list(patterns.keys()),
            "Count": list(patterns.values())
        })
        return response, pattern_df
    
    submit_btn.click(
        fn=process_and_analyze,
        inputs=input_text,
        outputs=[output_text, pattern_display]
    )

    demo.launch()

if __name__ == "__main__":
    demo.launch()