MalikShehram commited on
Commit
f947381
·
verified ·
1 Parent(s): f29dc1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -23
app.py CHANGED
@@ -5,35 +5,125 @@ from dotenv import load_dotenv
5
 
6
  # Load environment variables
7
  load_dotenv()
8
- BLACKBOX_API_KEY = os.getenv("sk-Ro0qnkz8DKLBGsuvsDRPaQ")
9
 
10
- def generate_text(prompt):
11
- # Call the Blackbox AI API
 
 
 
 
 
 
 
12
  headers = {
13
- "Authorization": f"Bearer {BLACKBOX_API_KEY}",
14
- "Content-Type": "application/json"
15
  }
16
- data = {
17
- "prompt": prompt,
18
- "max_tokens": 150, # Adjust as needed
19
- "temperature": 0.7 # Adjust as needed
 
 
 
20
  }
21
 
22
- response = requests.post("https://api.blackbox.ai/generate", headers=headers, json=data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- if response.status_code == 200:
25
- return response.json().get("text", "No response text found.")
26
- else:
27
- return f"Error: {response.status_code} - {response.text}"
28
 
29
- # Create Gradio interface
30
- iface = gr.Interface(
31
- fn=generate_text,
32
- inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
33
- outputs="text",
34
- title="Text-to-Text Copilot",
35
- description="A copilot for text generation that learns from coding patterns and provides suggestions."
36
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  if __name__ == "__main__":
39
- iface.launch()
 
 
 
 
 
 
5
 
6
  # Load environment variables
7
  load_dotenv()
 
8
 
9
+ # Constants
10
+ BLACKBOX_API_ENDPOINT = "https://www.blackbox.ai/api/chat"
11
+ BLACKBOX_API_KEY = os.getenv("sk--yuitsASMpfxFL4jNViUGw", "")
12
+
13
+ def generate_text_with_blackbox(prompt):
14
+ """Generate text using Blackbox AI API with proper error handling"""
15
+ if not BLACKBOX_API_KEY:
16
+ return "Error: BLACKBOX_API_KEY not found in environment variables"
17
+
18
  headers = {
19
+ "Content-Type": "application/json",
20
+ "Authorization": f"Bearer {BLACKBOX_API_KEY}"
21
  }
22
+
23
+ payload = {
24
+ "messages": [{"role": "user", "content": prompt}],
25
+ "id": "code-copilot-session",
26
+ "userId": "gradio-chat-interface",
27
+ "codeModelMode": True,
28
+ "previewToken": None
29
  }
30
 
31
+ try:
32
+ response = requests.post(
33
+ BLACKBOX_API_ENDPOINT,
34
+ headers=headers,
35
+ json=payload,
36
+ timeout=30
37
+ )
38
+
39
+ # Check for successful response
40
+ response.raise_for_status()
41
+
42
+ # Parse the response
43
+ if response.status_code == 200:
44
+ try:
45
+ return response.json().get("content", "No content returned")
46
+ except ValueError:
47
+ return "Error parsing JSON response"
48
+ else:
49
+ return f"API Error: {response.status_code} - {response.text}"
50
+
51
+ except requests.exceptions.RequestException as e:
52
+ return f"Request failed: {str(e)}"
53
+ except Exception as e:
54
+ return f"Unexpected error: {str(e)}"
55
+
56
+ def analyze_code(code):
57
+ """Analyze code and provide suggestions"""
58
+ analysis_prompt = f"""
59
+ Analyze this code and provide detailed suggestions for improvement:
60
+ - Code quality
61
+ - Optimizations
62
+ - Best practices
63
+ - Security considerations
64
+ - Readability enhancements
65
+
66
+ Code to analyze:
67
+ {code}
68
 
69
+ Respond with clear bullet points and explanations.
70
+ """
71
+ return generate_text_with_blackbox(analysis_prompt)
 
72
 
73
+ with gr.Blocks(title="AI Code Copilot", theme=gr.themes.Soft()) as app:
74
+ with gr.Row():
75
+ gr.Markdown("# 🚀 AI Code Copilot")
76
+ gr.Markdown("Powered by Blackbox AI")
77
+
78
+ with gr.Tabs():
79
+ with gr.TabItem("Code Generation"):
80
+ with gr.Column():
81
+ prompt = gr.Textbox(
82
+ label="Enter your prompt",
83
+ placeholder="Describe what code you want to generate...",
84
+ lines=3
85
+ )
86
+ generate_btn = gr.Button("Generate Code")
87
+ output = gr.Code(
88
+ label="Generated Code",
89
+ language="python",
90
+ interactive=True
91
+ )
92
+
93
+ with gr.TabItem("Code Analysis"):
94
+ with gr.Column():
95
+ code_input = gr.Code(
96
+ label="Paste your code here",
97
+ language="python",
98
+ lines=10
99
+ )
100
+ analyze_btn = gr.Button("Analyze Code")
101
+ suggestions = gr.Markdown(
102
+ label="Suggestions",
103
+ value="Your suggestions will appear here..."
104
+ )
105
+
106
+ generate_btn.click(
107
+ fn=generate_text_with_blackbox,
108
+ inputs=prompt,
109
+ outputs=output
110
+ )
111
+
112
+ analyze_btn.click(
113
+ fn=analyze_code,
114
+ inputs=code_input,
115
+ outputs=suggestions
116
+ )
117
+
118
+ gr.Markdown("---")
119
+ gr.Markdown("### Note:")
120
+ gr.Markdown("1. Make sure you have a valid BLACKBOX_API_KEY in your `.env` file")
121
+ gr.Markdown("2. Be specific with your prompts for better results")
122
 
123
  if __name__ == "__main__":
124
+ app.launch(
125
+ server_name="0.0.0.0",
126
+ server_port=7860,
127
+ show_error=True,
128
+ debug=True
129
+ )