Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +5 -8
- app.py +224 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
-
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AMK AI Coder Backend
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
from functools import lru_cache
|
| 10 |
+
|
| 11 |
+
import httpx
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from gradio import Blocks, Textbox, Dropdown, Button, Chatbot
|
| 14 |
+
|
| 15 |
+
# Configuration from environment
|
| 16 |
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
|
| 17 |
+
OPENROUTER_API_URL = "https://openrouter.ai/api/v1"
|
| 18 |
+
DEFAULT_MODEL = "anthropic/claude-3.5-sonnet"
|
| 19 |
+
|
| 20 |
+
# Deep thinking system prompt
|
| 21 |
+
DEEP_THINKING_PROMPT = """You are an advanced AI coding assistant with enhanced deep thinking capabilities.
|
| 22 |
+
When analyzing problems, take time to:
|
| 23 |
+
1. Understand the core requirements
|
| 24 |
+
2. Consider edge cases and potential issues
|
| 25 |
+
3. Plan a structured approach
|
| 26 |
+
4. Write clean, efficient, and well-documented code
|
| 27 |
+
5. Explain your reasoning step by step
|
| 28 |
+
|
| 29 |
+
Provide comprehensive, thoughtful responses that demonstrate deep analysis."""
|
| 30 |
+
|
| 31 |
+
STANDARD_PROMPT = """You are an AI coding assistant. Provide helpful, accurate, and efficient responses.
|
| 32 |
+
Write clean, well-documented code with appropriate comments."""
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class ChatSession:
|
| 36 |
+
"""Manages chat sessions with AI providers"""
|
| 37 |
+
|
| 38 |
+
def __init__(self, api_key: str = ""):
|
| 39 |
+
self.api_key = api_key or OPENROUTER_API_KEY
|
| 40 |
+
self.messages: List[Dict[str, str]] = []
|
| 41 |
+
self.use_deep_thinking = False
|
| 42 |
+
|
| 43 |
+
def add_message(self, role: str, content: str):
|
| 44 |
+
self.messages.append({"role": role, "content": content})
|
| 45 |
+
|
| 46 |
+
def get_context(self, system_prompt: str = STANDARD_PROMPT) -> List[Dict[str, str]]:
|
| 47 |
+
context = [{"role": "system", "content": system_prompt}]
|
| 48 |
+
context.extend(self.messages[-20:]) # Keep last 20 messages
|
| 49 |
+
return context
|
| 50 |
+
|
| 51 |
+
async def send_to_openrouter(
|
| 52 |
+
self,
|
| 53 |
+
message: str,
|
| 54 |
+
model: str = DEFAULT_MODEL,
|
| 55 |
+
stream: bool = True
|
| 56 |
+
) -> str:
|
| 57 |
+
"""Send message to OpenRouter API"""
|
| 58 |
+
if not self.api_key:
|
| 59 |
+
return "Error: OpenRouter API key not configured. Please set OPENROUTER_API_KEY."
|
| 60 |
+
|
| 61 |
+
self.add_message("user", message)
|
| 62 |
+
|
| 63 |
+
system_prompt = DEEP_THINKING_PROMPT if self.use_deep_thinking else STANDARD_PROMPT
|
| 64 |
+
payload = {
|
| 65 |
+
"model": model,
|
| 66 |
+
"messages": self.get_context(system_prompt),
|
| 67 |
+
"stream": stream,
|
| 68 |
+
"temperature": 0.7,
|
| 69 |
+
"max_tokens": 4096
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
headers = {
|
| 73 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 74 |
+
"Content-Type": "application/json",
|
| 75 |
+
"HTTP-Referer": "https://amk-coder.vercel.app",
|
| 76 |
+
"X-Title": "AMK AI Coder Platform"
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
try:
|
| 80 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 81 |
+
response = await client.post(
|
| 82 |
+
f"{OPENROUTER_API_URL}/chat/completions",
|
| 83 |
+
headers=headers,
|
| 84 |
+
json=payload
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if response.status_code == 200:
|
| 88 |
+
data = response.json()
|
| 89 |
+
assistant_message = data["choices"][0]["message"]["content"]
|
| 90 |
+
self.add_message("assistant", assistant_message)
|
| 91 |
+
return assistant_message
|
| 92 |
+
else:
|
| 93 |
+
error_msg = f"API Error: {response.status_code} - {response.text}"
|
| 94 |
+
return error_msg
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return f"Error: {str(e)}"
|
| 98 |
+
|
| 99 |
+
def clear(self):
|
| 100 |
+
self.messages = []
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# Global chat session
|
| 104 |
+
chat_session = ChatSession()
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def toggle_deep_thinking(enabled: bool):
|
| 108 |
+
"""Toggle deep thinking mode"""
|
| 109 |
+
chat_session.use_deep_thinking = enabled
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
async def chat_response(
|
| 113 |
+
message: str,
|
| 114 |
+
history: List[List[str]],
|
| 115 |
+
model: str,
|
| 116 |
+
deep_thinking: bool
|
| 117 |
+
) -> tuple:
|
| 118 |
+
"""Generate chat response"""
|
| 119 |
+
chat_session.use_deep_thinking = deep_thinking
|
| 120 |
+
|
| 121 |
+
response = await chat_session.send_to_openrouter(
|
| 122 |
+
message=message,
|
| 123 |
+
model=model,
|
| 124 |
+
stream=False
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
history.append([message, response])
|
| 128 |
+
return "", history
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def clear_chat():
|
| 132 |
+
"""Clear chat history"""
|
| 133 |
+
chat_session.clear()
|
| 134 |
+
return []
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
# Gradio UI
|
| 138 |
+
with gr.Blocks(
|
| 139 |
+
title="AMK AI Coder Backend",
|
| 140 |
+
theme=gr.themes.Soft(
|
| 141 |
+
primary_hue="indigo",
|
| 142 |
+
secondary_hue="purple",
|
| 143 |
+
)
|
| 144 |
+
) as demo:
|
| 145 |
+
gr.Markdown("""
|
| 146 |
+
# 🤖 AMK AI Coder Backend API
|
| 147 |
+
|
| 148 |
+
Powered by **OpenRouter** with support for multiple AI models.
|
| 149 |
+
|
| 150 |
+
### Features:
|
| 151 |
+
- 💬 Chat with AI models
|
| 152 |
+
- 🧠 Deep Thinking Mode for complex problems
|
| 153 |
+
- 📝 Code-focused responses
|
| 154 |
+
""")
|
| 155 |
+
|
| 156 |
+
with gr.Row():
|
| 157 |
+
with gr.Column(scale=3):
|
| 158 |
+
chatbot = gr.Chatbot(label="Chat", height=400)
|
| 159 |
+
|
| 160 |
+
with gr.Row():
|
| 161 |
+
msg = gr.Textbox(
|
| 162 |
+
placeholder="Ask me anything about coding...",
|
| 163 |
+
scale=4,
|
| 164 |
+
lines=3
|
| 165 |
+
)
|
| 166 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 167 |
+
|
| 168 |
+
with gr.Column(scale=1):
|
| 169 |
+
model_dropdown = gr.Dropdown(
|
| 170 |
+
choices=[
|
| 171 |
+
"anthropic/claude-3.5-sonnet",
|
| 172 |
+
"anthropic/claude-3-haiku",
|
| 173 |
+
"openai/gpt-4-turbo",
|
| 174 |
+
"google/gemini-pro",
|
| 175 |
+
"mistralai/mixtral-8x7b-instruct"
|
| 176 |
+
],
|
| 177 |
+
value="anthropic/claude-3.5-sonnet",
|
| 178 |
+
label="AI Model"
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
deep_thinking = gr.Checkbox(
|
| 182 |
+
label="🧠 Deep Thinking Mode",
|
| 183 |
+
info="Enable for complex problem solving"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
clear_btn = gr.Button("🗑️ Clear Chat", variant="secondary")
|
| 187 |
+
|
| 188 |
+
gr.Markdown("""
|
| 189 |
+
### API Endpoints
|
| 190 |
+
|
| 191 |
+
This backend provides:
|
| 192 |
+
- `/chat` - Chat completions
|
| 193 |
+
- `/models` - Available models
|
| 194 |
+
|
| 195 |
+
### Setup
|
| 196 |
+
|
| 197 |
+
Set `OPENROUTER_API_KEY` in environment variables.
|
| 198 |
+
""")
|
| 199 |
+
|
| 200 |
+
# Event handlers
|
| 201 |
+
submit_btn.click(
|
| 202 |
+
fn=chat_response,
|
| 203 |
+
inputs=[msg, chatbot, model_dropdown, deep_thinking],
|
| 204 |
+
outputs=[msg, chatbot]
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
msg.submit(
|
| 208 |
+
fn=chat_response,
|
| 209 |
+
inputs=[msg, chatbot, model_dropdown, deep_thinking],
|
| 210 |
+
outputs=[msg, chatbot]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
clear_btn.click(
|
| 214 |
+
fn=clear_chat,
|
| 215 |
+
outputs=[chatbot]
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
# Launch
|
| 219 |
+
if __name__ == "__main__":
|
| 220 |
+
demo.launch(
|
| 221 |
+
server_name="0.0.0.0",
|
| 222 |
+
server_port=7860,
|
| 223 |
+
share=False
|
| 224 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
httpx>=0.25.0
|