Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| # Constants | |
| BLACKBOX_API_ENDPOINT = "https://www.blackbox.ai/api/chat" | |
| BLACKBOX_API_KEY = os.getenv("sk--yuitsASMpfxFL4jNViUGw", "") | |
| def generate_text_with_blackbox(prompt): | |
| """Generate text using Blackbox AI API with proper error handling""" | |
| if not BLACKBOX_API_KEY: | |
| return "Error: BLACKBOX_API_KEY not found in environment variables" | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Authorization": f"Bearer {BLACKBOX_API_KEY}" | |
| } | |
| payload = { | |
| "messages": [{"role": "user", "content": prompt}], | |
| "id": "code-copilot-session", | |
| "userId": "gradio-chat-interface", | |
| "codeModelMode": True, | |
| "previewToken": None | |
| } | |
| try: | |
| response = requests.post( | |
| BLACKBOX_API_ENDPOINT, | |
| headers=headers, | |
| json=payload, | |
| timeout=30 | |
| ) | |
| # Check for successful response | |
| response.raise_for_status() | |
| # Parse the response | |
| if response.status_code == 200: | |
| try: | |
| return response.json().get("content", "No content returned") | |
| except ValueError: | |
| return "Error parsing JSON response" | |
| else: | |
| return f"API Error: {response.status_code} - {response.text}" | |
| except requests.exceptions.RequestException as e: | |
| return f"Request failed: {str(e)}" | |
| except Exception as e: | |
| return f"Unexpected error: {str(e)}" | |
| def analyze_code(code): | |
| """Analyze code and provide suggestions""" | |
| analysis_prompt = f""" | |
| Analyze this code and provide detailed suggestions for improvement: | |
| - Code quality | |
| - Optimizations | |
| - Best practices | |
| - Security considerations | |
| - Readability enhancements | |
| Code to analyze: | |
| {code} | |
| Respond with clear bullet points and explanations. | |
| """ | |
| return generate_text_with_blackbox(analysis_prompt) | |
| with gr.Blocks(title="AI Code Copilot", theme=gr.themes.Soft()) as app: | |
| with gr.Row(): | |
| gr.Markdown("# ๐ AI Code Copilot") | |
| gr.Markdown("Powered by Blackbox AI") | |
| with gr.Tabs(): | |
| with gr.TabItem("Code Generation"): | |
| with gr.Column(): | |
| prompt = gr.Textbox( | |
| label="Enter your prompt", | |
| placeholder="Describe what code you want to generate...", | |
| lines=3 | |
| ) | |
| generate_btn = gr.Button("Generate Code") | |
| output = gr.Code( | |
| label="Generated Code", | |
| language="python", | |
| interactive=True | |
| ) | |
| with gr.TabItem("Code Analysis"): | |
| with gr.Column(): | |
| code_input = gr.Code( | |
| label="Paste your code here", | |
| language="python", | |
| lines=10 | |
| ) | |
| analyze_btn = gr.Button("Analyze Code") | |
| suggestions = gr.Markdown( | |
| label="Suggestions", | |
| value="Your suggestions will appear here..." | |
| ) | |
| generate_btn.click( | |
| fn=generate_text_with_blackbox, | |
| inputs=prompt, | |
| outputs=output | |
| ) | |
| analyze_btn.click( | |
| fn=analyze_code, | |
| inputs=code_input, | |
| outputs=suggestions | |
| ) | |
| gr.Markdown("---") | |
| gr.Markdown("### Note:") | |
| gr.Markdown("1. Make sure you have a valid BLACKBOX_API_KEY in your `.env` file") | |
| gr.Markdown("2. Be specific with your prompts for better results") | |
| if __name__ == "__main__": | |
| app.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| show_error=True, | |
| debug=True | |
| ) | |