Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Configure the page
|
| 7 |
+
st.set_page_config(page_title="NanoCoder AI", page_icon="💻", layout="wide")
|
| 8 |
+
|
| 9 |
+
# Retrieve OpenRouter API Key from Hugging Face Secrets
|
| 10 |
+
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
|
| 11 |
+
|
| 12 |
+
st.title("💻 NanoCoder Web App")
|
| 13 |
+
st.caption("Powered by Hugging Face Spaces & OpenRouter Free Models")
|
| 14 |
+
|
| 15 |
+
# Sidebar for settings
|
| 16 |
+
st.sidebar.header("Configuration")
|
| 17 |
+
model_option = st.sidebar.selectbox(
|
| 18 |
+
"Choose Free Coding Model:",
|
| 19 |
+
[
|
| 20 |
+
"nvidia/nemotron-3-super-120b-a12b:free",
|
| 21 |
+
"poolside/laguna-m.1:free",
|
| 22 |
+
"minimax/minimax-m2.5:free",
|
| 23 |
+
"openai/gpt-oss-120b:free"
|
| 24 |
+
]
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
system_prompt = st.sidebar.text_area(
|
| 28 |
+
"System Prompt",
|
| 29 |
+
"You are NanoCoder, a hyper-efficient, concise coding assistant. Provide clean, production-ready code blocks without unnecessary fluff."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Initialize chat history
|
| 33 |
+
if "messages" not in st.session_state:
|
| 34 |
+
st.session_state.messages = []
|
| 35 |
+
|
| 36 |
+
# Display chat history
|
| 37 |
+
for message in st.session_state.messages:
|
| 38 |
+
with st.chat_message(message["role"]):
|
| 39 |
+
st.markdown(message["content"])
|
| 40 |
+
|
| 41 |
+
# Accept user input
|
| 42 |
+
if prompt := st.chat_input("What do you want to code today?"):
|
| 43 |
+
# Check if API key is present
|
| 44 |
+
if not OPENROUTER_API_KEY:
|
| 45 |
+
st.error("Missing OpenRouter API Key! Please set it up in your Space settings.")
|
| 46 |
+
st.stop()
|
| 47 |
+
|
| 48 |
+
# Append user message
|
| 49 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 50 |
+
with st.chat_message("user"):
|
| 51 |
+
st.markdown(prompt)
|
| 52 |
+
|
| 53 |
+
# Call OpenRouter API
|
| 54 |
+
with st.chat_message("assistant"):
|
| 55 |
+
response_placeholder = st.empty()
|
| 56 |
+
response_placeholder.markdown("*Thinking...*")
|
| 57 |
+
|
| 58 |
+
headers = {
|
| 59 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 60 |
+
"HTTP-Referer": "https://huggingface.co/spaces", # Required by OpenRouter
|
| 61 |
+
"Content-Type": "application/json"
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
# Format the context payload
|
| 65 |
+
api_messages = [{"role": "system", "content": system_prompt}] + [
|
| 66 |
+
{"role": m["role"], "content": m["content"]} for m in st.session_state.messages
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
payload = {
|
| 70 |
+
"model": model_option,
|
| 71 |
+
"messages": api_messages
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
res = requests.post(
|
| 76 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 77 |
+
headers=headers,
|
| 78 |
+
data=json.dumps(payload),
|
| 79 |
+
timeout=30
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if res.status_code == 200:
|
| 83 |
+
response_json = res.json()
|
| 84 |
+
assistant_response = response_json['choices'][0]['message']['content']
|
| 85 |
+
response_placeholder.markdown(assistant_response)
|
| 86 |
+
# Save assistant response to history
|
| 87 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 88 |
+
else:
|
| 89 |
+
response_placeholder.markdown(f"⚠️ Error: {res.status_code} - {res.text}")
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
response_placeholder.markdown(f"⚠️ Failed to connect to OpenRouter: {str(e)}")
|