Spaces:
Sleeping
Sleeping
Download app.py from SEVAQWERTY/Free-claude-code: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/SEVAQWERTY/Free-claude-code/resolve/main/app.py
- Command line
-
hf download hf://spaces/SEVAQWERTY/Free-claude-code/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SEVAQWERTY/Free-claude-code/resolve/main/app.py
2.14 kB
| import os | |
| import requests | |
| import gradio as gr | |
| OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY") | |
| if not OPENROUTER_API_KEY: | |
| raise ValueError("Добавь OPENROUTER_API_KEY в Secrets") | |
| def ask_openrouter(message: str, history: list) -> str: | |
| # Преобразуем историю Gradio в формат messages | |
| messages = [] | |
| for user_msg, bot_msg in history: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if bot_msg: | |
| messages.append({"role": "assistant", "content": bot_msg}) | |
| # Добавляем текущий вопрос | |
| messages.append({"role": "user", "content": message}) | |
| try: | |
| response = requests.post( | |
| "https://openrouter.ai/api/v1/chat/completions", | |
| headers={ | |
| "Authorization": f"Bearer {OPENROUTER_API_KEY}", | |
| "Content-Type": "application/json", | |
| "HTTP-Referer": "https://huggingface.co", | |
| "X-Title": "Claude Code" | |
| }, | |
| json={ | |
| "model": "deepseek/deepseek-r1-0528:free", | |
| "messages": messages, # теперь точно правильный формат | |
| "max_tokens": 4096, | |
| "temperature": 0.7, | |
| "stream": False | |
| }, | |
| timeout=60 | |
| ) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return f"Ошибка API ({response.status_code}): {response.text[:300]}" | |
| except Exception as e: | |
| return f"Ошибка: {str(e)}" | |
| with gr.Blocks(title="Claude Code Free") as demo: | |
| gr.Markdown("# 🤖 Claude Code") | |
| chatbot = gr.Chatbot(height=500) | |
| msg = gr.Textbox(label="Запрос", placeholder="Пиши код...") | |
| clear = gr.ClearButton([msg, chatbot]) | |
| def respond(message, chat_history): | |
| reply = ask_openrouter(message, chat_history) | |
| chat_history.append((message, reply)) | |
| return "", chat_history | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| demo.launch() |