amkyawdev commited on
Commit
d95df27
·
verified ·
1 Parent(s): 50dbada

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. Dockerfile +16 -0
  2. README.md +3 -3
  3. app.py +109 -82
  4. app_fastapi.py +159 -0
  5. requirements.txt +4 -2
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy app
10
+ COPY app.py .
11
+
12
+ # Expose port
13
+ EXPOSE 7860
14
+
15
+ # Run
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -3,8 +3,8 @@ title: AMK AI Coder Backend
3
  emoji: 🤖
4
  colorFrom: purple
5
  colorTo: indigo
6
- sdk: streamlit
7
- sdk_version: 1.40.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
 
3
  emoji: 🤖
4
  colorFrom: purple
5
  colorTo: indigo
6
+ sdk: docker
7
+ app_file: Dockerfile
 
8
  pinned: false
9
+ hardware: t4-small
10
  ---
app.py CHANGED
@@ -1,18 +1,21 @@
1
  """
2
  AMK AI Coder Platform - Backend API
3
- Streamlit app for HuggingFace Spaces deployment
4
  """
5
  import os
6
- import asyncio
7
- from typing import List, Dict
 
8
 
9
- import streamlit as st
10
- import requests
 
 
11
 
12
  # Configuration
13
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
14
  OPENROUTER_API_URL = "https://openrouter.ai/api/v1"
15
- DEFAULT_MODEL = "inclusionai/ling-2.6-1t"
16
 
17
  # System prompts
18
  DEEP_THINKING_PROMPT = """You are an advanced AI coding assistant with enhanced deep thinking capabilities.
@@ -28,33 +31,82 @@ Provide comprehensive, thoughtful responses."""
28
  STANDARD_PROMPT = """You are an AI coding assistant. Provide helpful, accurate, and efficient responses.
29
  Write clean, well-documented code with appropriate comments."""
30
 
31
- # Page config
32
- st.set_page_config(page_title="AMK AI Coder Backend", page_icon="🤖")
33
 
34
- # Initialize session state
35
- if "messages" not in st.session_state:
36
- st.session_state.messages = []
37
- if "deep_thinking" not in st.session_state:
38
- st.session_state.deep_thinking = False
39
- if "model" not in st.session_state:
40
- st.session_state.model = DEFAULT_MODEL
41
 
42
- def send_to_openrouter(message: str, model: str, deep_thinking: bool) -> str:
43
- """Send message to OpenRouter API"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  if not OPENROUTER_API_KEY:
45
- return "Error: OpenRouter API key not configured. Please set OPENROUTER_API_KEY in Space settings."
 
 
 
 
 
 
 
46
 
47
- st.session_state.messages.append({"role": "user", "content": message})
 
48
 
49
- system_prompt = DEEP_THINKING_PROMPT if deep_thinking else STANDARD_PROMPT
 
50
 
51
- # Build messages with system prompt
52
- messages = [{"role": "system", "content": system_prompt}]
53
- messages.extend([m for m in st.session_state.messages if m["role"] != "system"][-20:])
54
 
 
55
  payload = {
56
- "model": model,
57
- "messages": messages,
58
  "temperature": 0.7,
59
  "max_tokens": 4096
60
  }
@@ -67,66 +119,41 @@ def send_to_openrouter(message: str, model: str, deep_thinking: bool) -> str:
67
  }
68
 
69
  try:
70
- response = requests.post(
71
- f"{OPENROUTER_API_URL}/chat/completions",
72
- headers=headers,
73
- json=payload,
74
- timeout=120
75
- )
76
-
77
- if response.status_code == 200:
 
 
78
  data = response.json()
79
  assistant_message = data["choices"][0]["message"]["content"]
80
- st.session_state.messages.append({"role": "assistant", "content": assistant_message})
81
- return assistant_message
82
- else:
83
- return f"API Error: {response.status_code} - {response.text}"
84
 
 
 
 
 
 
 
 
 
 
 
 
85
  except Exception as e:
86
- return f"Error: {str(e)}"
87
 
88
- # UI
89
- st.title("🤖 AMK AI Coder Backend")
90
- st.markdown("Powered by **OpenRouter** with ling-2.6-1t model")
 
 
 
91
 
92
- # Sidebar settings
93
- with st.sidebar:
94
- st.header("Settings")
95
-
96
- model = st.selectbox(
97
- "AI Model",
98
- [
99
- "inclusionai/ling-2.6-1t:free",
100
- "anthropic/claude-3.5-sonnet",
101
- "google/gemini-pro",
102
- ],
103
- index=0
104
- )
105
- st.session_state.model = model
106
-
107
- deep_thinking = st.checkbox("🧠 Deep Thinking Mode", value=st.session_state.deep_thinking)
108
- st.session_state.deep_thinking = deep_thinking
109
-
110
- if st.button("🗑️ Clear Chat"):
111
- st.session_state.messages = []
112
- st.rerun()
113
-
114
- # Display chat history
115
- for message in st.session_state.messages:
116
- if message["role"] != "system":
117
- with st.chat_message(message["role"]):
118
- st.markdown(message["content"])
119
-
120
- # Chat input
121
- if prompt := st.chat_input("Ask me anything..."):
122
- with st.chat_message("user"):
123
- st.markdown(prompt)
124
-
125
- with st.chat_message("assistant"):
126
- with st.spinner("Thinking..."):
127
- response = send_to_openrouter(prompt, st.session_state.model, st.session_state.deep_thinking)
128
- st.markdown(response)
129
-
130
- # API Key warning
131
- if not OPENROUTER_API_KEY:
132
- st.warning("⚠️ OpenRouter API key not set. Please add OPENROUTER_API_KEY in Space settings.")
 
1
  """
2
  AMK AI Coder Platform - Backend API
3
+ FastAPI for HuggingFace Spaces
4
  """
5
  import os
6
+ import json
7
+ from typing import List, Optional, Dict
8
+ from contextlib import asynccontextmanager
9
 
10
+ from fastapi import FastAPI, HTTPException
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ from pydantic import BaseModel
13
+ import httpx
14
 
15
  # Configuration
16
  OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
17
  OPENROUTER_API_URL = "https://openrouter.ai/api/v1"
18
+ DEFAULT_MODEL = "inclusionai/ling-2.6-1t:free"
19
 
20
  # System prompts
21
  DEEP_THINKING_PROMPT = """You are an advanced AI coding assistant with enhanced deep thinking capabilities.
 
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
+ # In-memory conversation storage
35
+ conversations: Dict[str, List[dict]] = {}
36
 
37
+ @asynccontextmanager
38
+ async def lifespan(app: FastAPI):
39
+ print("AMK AI Coder Backend started!")
40
+ yield
 
 
 
41
 
42
+ # Create FastAPI app
43
+ app = FastAPI(
44
+ title="AMK AI Coder Backend API",
45
+ description="Backend API for AMK AI Coder Platform",
46
+ version="1.0.0",
47
+ lifespan=lifespan
48
+ )
49
+
50
+ # CORS
51
+ app.add_middleware(
52
+ CORSMiddleware,
53
+ allow_origins=["*"],
54
+ allow_credentials=True,
55
+ allow_methods=["*"],
56
+ allow_headers=["*"],
57
+ )
58
+
59
+ # Models
60
+ class ChatRequest(BaseModel):
61
+ message: str
62
+ session_id: Optional[str] = "default"
63
+ thinking_mode: bool = False
64
+ model: str = DEFAULT_MODEL
65
+
66
+ class ChatResponse(BaseModel):
67
+ response: str
68
+ session_id: str
69
+ thinking_mode: bool
70
+
71
+ # API Endpoints
72
+ @app.get("/")
73
+ async def root():
74
+ return {"status": "ok", "message": "AMK AI Coder Backend API", "model": DEFAULT_MODEL}
75
+
76
+ @app.get("/health")
77
+ async def health():
78
+ return {
79
+ "status": "healthy",
80
+ "api_key_configured": bool(OPENROUTER_API_KEY)
81
+ }
82
+
83
+ @app.post("/api/chat", response_model=ChatResponse)
84
+ async def chat(request: ChatRequest):
85
+ """Main chat endpoint"""
86
  if not OPENROUTER_API_KEY:
87
+ raise HTTPException(status_code=500, detail="OpenRouter API key not configured")
88
+
89
+ # Get or create conversation
90
+ session_id = request.session_id
91
+ if session_id not in conversations:
92
+ conversations[session_id] = []
93
+
94
+ messages = conversations[session_id]
95
 
96
+ # Add user message
97
+ messages.append({"role": "user", "content": request.message})
98
 
99
+ # Build system prompt
100
+ system_prompt = DEEP_THINKING_PROMPT if request.thinking_mode else STANDARD_PROMPT
101
 
102
+ # Build full context
103
+ all_messages = [{"role": "system", "content": system_prompt}]
104
+ all_messages.extend(messages[-20:]) # Keep last 20 messages
105
 
106
+ # Call OpenRouter
107
  payload = {
108
+ "model": request.model,
109
+ "messages": all_messages,
110
  "temperature": 0.7,
111
  "max_tokens": 4096
112
  }
 
119
  }
120
 
121
  try:
122
+ async with httpx.AsyncClient(timeout=120.0) as client:
123
+ response = await client.post(
124
+ f"{OPENROUTER_API_URL}/chat/completions",
125
+ headers=headers,
126
+ json=payload
127
+ )
128
+
129
+ if response.status_code != 200:
130
+ raise HTTPException(status_code=response.status_code, detail=response.text)
131
+
132
  data = response.json()
133
  assistant_message = data["choices"][0]["message"]["content"]
 
 
 
 
134
 
135
+ # Add to conversation history
136
+ messages.append({"role": "assistant", "content": assistant_message})
137
+
138
+ return ChatResponse(
139
+ response=assistant_message,
140
+ session_id=session_id,
141
+ thinking_mode=request.thinking_mode
142
+ )
143
+
144
+ except httpx.TimeoutException:
145
+ raise HTTPException(status_code=504, detail="Request timeout")
146
  except Exception as e:
147
+ raise HTTPException(status_code=500, detail=str(e))
148
 
149
+ @app.post("/api/clear")
150
+ async def clear_chat(session_id: str = "default"):
151
+ """Clear conversation history"""
152
+ if session_id in conversations:
153
+ conversations[session_id] = []
154
+ return {"status": "cleared", "session_id": session_id}
155
 
156
+ @app.get("/api/history/{session_id}")
157
+ async def get_history(session_id: str = "default"):
158
+ """Get conversation history"""
159
+ return {"session_id": session_id, "messages": conversations.get(session_id, [])}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_fastapi.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ AMK AI Coder Platform - Backend API
3
+ FastAPI for HuggingFace Spaces
4
+ """
5
+ import os
6
+ import json
7
+ from typing import List, Optional, Dict
8
+ from contextlib import asynccontextmanager
9
+
10
+ from fastapi import FastAPI, HTTPException
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ from pydantic import BaseModel
13
+ import httpx
14
+
15
+ # Configuration
16
+ OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
17
+ OPENROUTER_API_URL = "https://openrouter.ai/api/v1"
18
+ DEFAULT_MODEL = "inclusionai/ling-2.6-1t:free"
19
+
20
+ # System prompts
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."""
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
+ # In-memory conversation storage
35
+ conversations: Dict[str, List[dict]] = {}
36
+
37
+ @asynccontextmanager
38
+ async def lifespan(app: FastAPI):
39
+ print("AMK AI Coder Backend started!")
40
+ yield
41
+
42
+ # Create FastAPI app
43
+ app = FastAPI(
44
+ title="AMK AI Coder Backend API",
45
+ description="Backend API for AMK AI Coder Platform",
46
+ version="1.0.0",
47
+ lifespan=lifespan
48
+ )
49
+
50
+ # CORS
51
+ app.add_middleware(
52
+ CORSMiddleware,
53
+ allow_origins=["*"],
54
+ allow_credentials=True,
55
+ allow_methods=["*"],
56
+ allow_headers=["*"],
57
+ )
58
+
59
+ # Models
60
+ class ChatRequest(BaseModel):
61
+ message: str
62
+ session_id: Optional[str] = "default"
63
+ thinking_mode: bool = False
64
+ model: str = DEFAULT_MODEL
65
+
66
+ class ChatResponse(BaseModel):
67
+ response: str
68
+ session_id: str
69
+ thinking_mode: bool
70
+
71
+ # API Endpoints
72
+ @app.get("/")
73
+ async def root():
74
+ return {"status": "ok", "message": "AMK AI Coder Backend API", "model": DEFAULT_MODEL}
75
+
76
+ @app.get("/health")
77
+ async def health():
78
+ return {
79
+ "status": "healthy",
80
+ "api_key_configured": bool(OPENROUTER_API_KEY)
81
+ }
82
+
83
+ @app.post("/api/chat", response_model=ChatResponse)
84
+ async def chat(request: ChatRequest):
85
+ """Main chat endpoint"""
86
+ if not OPENROUTER_API_KEY:
87
+ raise HTTPException(status_code=500, detail="OpenRouter API key not configured")
88
+
89
+ # Get or create conversation
90
+ session_id = request.session_id
91
+ if session_id not in conversations:
92
+ conversations[session_id] = []
93
+
94
+ messages = conversations[session_id]
95
+
96
+ # Add user message
97
+ messages.append({"role": "user", "content": request.message})
98
+
99
+ # Build system prompt
100
+ system_prompt = DEEP_THINKING_PROMPT if request.thinking_mode else STANDARD_PROMPT
101
+
102
+ # Build full context
103
+ all_messages = [{"role": "system", "content": system_prompt}]
104
+ all_messages.extend(messages[-20:]) # Keep last 20 messages
105
+
106
+ # Call OpenRouter
107
+ payload = {
108
+ "model": request.model,
109
+ "messages": all_messages,
110
+ "temperature": 0.7,
111
+ "max_tokens": 4096
112
+ }
113
+
114
+ headers = {
115
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
116
+ "Content-Type": "application/json",
117
+ "HTTP-Referer": "https://amk-coder.vercel.app",
118
+ "X-Title": "AMK AI Coder Platform"
119
+ }
120
+
121
+ try:
122
+ async with httpx.AsyncClient(timeout=120.0) as client:
123
+ response = await client.post(
124
+ f"{OPENROUTER_API_URL}/chat/completions",
125
+ headers=headers,
126
+ json=payload
127
+ )
128
+
129
+ if response.status_code != 200:
130
+ raise HTTPException(status_code=response.status_code, detail=response.text)
131
+
132
+ data = response.json()
133
+ assistant_message = data["choices"][0]["message"]["content"]
134
+
135
+ # Add to conversation history
136
+ messages.append({"role": "assistant", "content": assistant_message})
137
+
138
+ return ChatResponse(
139
+ response=assistant_message,
140
+ session_id=session_id,
141
+ thinking_mode=request.thinking_mode
142
+ )
143
+
144
+ except httpx.TimeoutException:
145
+ raise HTTPException(status_code=504, detail="Request timeout")
146
+ except Exception as e:
147
+ raise HTTPException(status_code=500, detail=str(e))
148
+
149
+ @app.post("/api/clear")
150
+ async def clear_chat(session_id: str = "default"):
151
+ """Clear conversation history"""
152
+ if session_id in conversations:
153
+ conversations[session_id] = []
154
+ return {"status": "cleared", "session_id": session_id}
155
+
156
+ @app.get("/api/history/{session_id}")
157
+ async def get_history(session_id: str = "default"):
158
+ """Get conversation history"""
159
+ return {"session_id": session_id, "messages": conversations.get(session_id, [])}
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
- streamlit>=1.40.0
2
- requests>=2.31.0
 
 
 
1
+ fastapi>=0.100.0
2
+ uvicorn[standard]>=0.23.0
3
+ httpx>=0.25.0
4
+ pydantic>=2.0.0