# Copyright Volkan Sah aka AliBrown # Free to use with your own api-key import os import streamlit as st import tempfile import requests import json from datetime import datetime # ---------------------------------------------------- # ๐จ HUGGINGFACE SPACES FIX # ---------------------------------------------------- TEMP_STREAMLIT_HOME = os.path.join(tempfile.gettempdir(), "st_config_workaround") os.makedirs(TEMP_STREAMLIT_HOME, exist_ok=True) os.environ["STREAMLIT_HOME"] = TEMP_STREAMLIT_HOME os.environ["STREAMLIT_GATHER_USAGE_STATS"] = "false" CONFIG_PATH = os.path.join(TEMP_STREAMLIT_HOME, "config.toml") CONFIG_CONTENT = """ [browser] gatherUsageStats = false """ if not os.path.exists(CONFIG_PATH): try: with open(CONFIG_PATH, "w") as f: f.write(CONFIG_CONTENT) except Exception: pass # ---------------------------------------------------- # CONFIG # ---------------------------------------------------- st.set_page_config( page_title="AI Code Forge", layout="wide", page_icon="โ๏ธ" ) OPENROUTER_API_BASE = "https://openrouter.ai/api/v1" FREE_MODELS = { "๐ง DeepSeek Chat": "deepseek/deepseek-chat-v3.1:free", "๐ป Qwen Coder": "qwen/qwen3-coder:free", "๐ฎ Gemma 27B": "google/gemma-3-27b-it:free", "๐ฌ Dolphin Mistral": "cognitivecomputations/dolphin-mistral-24b-venice-edition:free", "โก Nemotron Nano": "nvidia/nemotron-nano-9b-v2:free", } # ---------------------------------------------------- # SESSION STATE # ---------------------------------------------------- if "messages" not in st.session_state: st.session_state.messages = {} if "current_tool" not in st.session_state: st.session_state.current_tool = "home" if "last_output" not in st.session_state: st.session_state.last_output = "" if "dark_mode" not in st.session_state: st.session_state.dark_mode = True # ---------------------------------------------------- # HELPER FUNCTIONS # ---------------------------------------------------- def fetch_model_contexts(api_key): if not api_key: return {} headers = {"Authorization": f"Bearer {api_key}"} try: res = requests.get(f"{OPENROUTER_API_BASE}/models", headers=headers, timeout=10) contexts = {} if res.status_code == 200: for m in res.json().get("data", []): contexts[m.get("id")] = m.get("context_length", 4096) return contexts except Exception: return {} def call_openrouter(model, messages, temp, max_tok, key, system_prompt=None): headers = { "Authorization": f"Bearer {key}", "Content-Type": "application/json", "Referer": "https://aicodecraft.io", "X-Title": "AI-Code-Forge", } api_messages = messages.copy() if system_prompt: api_messages.insert(0, {"role": "system", "content": system_prompt}) payload = { "model": model, "messages": api_messages, "temperature": temp, "max_tokens": max_tok, } res = requests.post( f"{OPENROUTER_API_BASE}/chat/completions", headers=headers, data=json.dumps(payload), timeout=60 ) if res.status_code == 200: try: return res.json()["choices"][0]["message"]["content"] except (KeyError, IndexError): raise Exception("Fehlerhafte API-Antwort") else: try: err = res.json() msg = err.get("error", {}).get("message", res.text) except: msg = res.text raise Exception(f"API Error {res.status_code}: {msg}") def extract_code_blocks(text): """Extrahiert Code aus Markdown Code-Blรถcken""" import re pattern = r"```(\w+)?\n(.*?)```" matches = re.findall(pattern, text, re.DOTALL) if matches: return [(lang or "text", code.strip()) for lang, code in matches] return [("text", text)] # ---------------------------------------------------- # TOOL PRESETS # ---------------------------------------------------- TOOL_PRESETS = { "code_gen": { "icon": "๐จ", "title": "Code Generator", "color": "#00ff88", "system_prompt": "You are an Expert Code Generator. Create clean, production-ready code with comments. Respond ONLY with code in Markdown code blocks.", "templates": { "Flask REST API": "Create a Flask REST API with:\n- CRUD Endpoints for 'tasks'\n- SQLAlchemy Models\n- Error Handling\n- CORS enabled", "React Component": "Create a React Functional Component with:\n- Props for title and items (Array)\n- useState for selected item\n- Tailwind CSS Styling\n- Click Handler", "Python CLI Tool": "Create a Python CLI Tool with:\n- argparse for Arguments\n- Logging\n- Error Handling\n- Main Guard", "FastAPI Endpoint": "Create FastAPI Endpoints:\n- POST /users - Create User\n- GET /users/{id} - Get User\n- Pydantic Models\n- SQLAlchemy Integration", } }, "refactor": { "icon": "๐", "title": "Code Refactorer", "color": "#00aaff", "system_prompt": "You are a Code Refactoring Expert. Analyze code and improve: readability, performance, best practices. Explain your changes briefly.", "templates": { "Clean Code": "Refactor this code according to Clean Code principles:\n- Improve Naming\n- Reduce Complexity\n- Extract Functions\n\n[Insert Code Here]", "Performance": "Optimize this code for performance:\n- Identify Bottlenecks\n- Improve Algorithms\n- Reduce Memory Usage\n\n[Insert Code Here]", "Type Safety": "Add Type Hints/TypeScript:\n- All Functions\n- Variables where appropriate\n- Return Types\n\n[Insert Code Here]", } }, "debug": { "icon": "๐", "title": "Debug Helper", "color": "#ff4444", "system_prompt": "You are a Debugging Expert. Analyze code/errors systematically. Explain the problem and provide the fix.", "templates": { "Error Analysis": "I have this error:\n[Error Message]\n\nIn this code:\n[Code]\n\nWhat is the problem and how do I fix it?", "Logic Bug": "This code doesn't do what it's supposed to:\n[Code]\n\nExpected behavior: [describe]\nActual behavior: [describe]\n\nWhere is the logic bug?", "Performance Issue": "This code is too slow:\n[Code]\n\nInput Size: [describe]\nCurrent Runtime: [describe]\n\nWhere is the problem?", } }, "docs": { "icon": "๐", "title": "Doc Generator", "color": "#ffaa00", "system_prompt": "You are a Technical Writer. Create clear, structured documentation with examples.", "templates": { "Function Docs": "Create Docstrings for these functions:\n[Code]\n\nFormat: Google Style for Python / JSDoc for JavaScript", "README": "Create a README.md for this project:\n- Description\n- Installation\n- Usage Examples\n- API Reference\n\nProject: [describe]", "API Docs": "Create API Documentation for these Endpoints:\n[Code/Routes]\n\nIncl. Request/Response Examples", } }, "explain": { "icon": "๐ก", "title": "Code Explainer", "color": "#aa88ff", "system_prompt": "You are a Code Educator. Explain code step-by-step, clearly and understandably.", "templates": { "ELI5": "Explain this code as if I were 5:\n[Code]", "Deep Dive": "Explain this code in detail:\n- What does it do?\n- How does it work?\n- Why was it solved this way?\n- Alternatives?\n\n[Code]", "Architecture": "Explain the architecture of this code:\n- Design Patterns\n- Components\n- Data Flow\n\n[Code]", } } } # ---------------------------------------------------- # SIDEBAR # ---------------------------------------------------- with st.sidebar: st.markdown("# โ๏ธ AI Code Forge") st.markdown("---") # API Settings with st.expander("๐ API Settings", expanded=True): api_key = st.text_input("OpenRouter API Key", type="password") model_name = st.selectbox("Model", list(FREE_MODELS.keys())) model = FREE_MODELS[model_name] model_contexts = fetch_model_contexts(api_key) default_ctx = model_contexts.get(model, 4096) temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1) max_tokens = st.slider( "Max Tokens", 256, min(default_ctx, 16000), min(2048, default_ctx), step=256 ) st.caption(f"๐ Context: {default_ctx:,} tokens") st.markdown("---") # Navigation st.markdown("### ๐งญ Tools") if st.button("๐ Home", use_container_width=True): st.session_state.current_tool = "home" st.rerun() for tool_id, tool in TOOL_PRESETS.items(): if st.button( f"{tool['icon']} {tool['title']}", use_container_width=True, type="primary" if st.session_state.current_tool == tool_id else "secondary" ): st.session_state.current_tool = tool_id if tool_id not in st.session_state.messages: st.session_state.messages[tool_id] = [] st.rerun() st.markdown("---") # Actions if st.session_state.current_tool != "home": if st.button("๐๏ธ Clear Chat", use_container_width=True): if st.session_state.current_tool in st.session_state.messages: st.session_state.messages[st.session_state.current_tool] = [] st.success("Chat cleared!") st.rerun() st.markdown("---") st.caption("๐ธ Using Free Models") st.caption(f"โฐ {datetime.now().strftime('%H:%M')}") # ---------------------------------------------------- # MAIN CONTENT # ---------------------------------------------------- # HOME if st.session_state.current_tool == "home": st.markdown("# โ๏ธ Welcome to AI Code Forge") st.markdown("### Your AI-Powered Development Assistant") st.markdown("---") # Tool Cards cols = st.columns(3) tool_items = list(TOOL_PRESETS.items()) for idx, (tool_id, tool) in enumerate(tool_items): with cols[idx % 3]: with st.container(): st.markdown(f"""
{tool['system_prompt'][:100]}...
{tool['system_prompt']}