hermes / app.py
oppssy's picture
Update app.py
6d8b2c1 verified
Raw
History Blame Contribute Delete
2.47 kB
import os
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from openai import OpenAI
app = FastAPI()
MODEL = "nousresearch/hermes-2-pro-llama-3-8b"
def get_client():
key = os.getenv("NVIDIA_API_KEY")
if not key:
raise RuntimeError("❌ NVIDIA_API_KEY missing. Add it in Settings β†’ Variables & secrets β†’ Factory reboot.")
return OpenAI(base_url="https://integrate.api.nvidia.com/v1", api_key=key)
@app.get("/", response_class=HTMLResponse)
async def home():
return """
<!DOCTYPE html>
<html><head><title>Hermes NIM</title>
<style>
body{font-family:system-ui,sans-serif;max-width:700px;margin:40px auto;padding:20px;background:#0f0f0f;color:#e0e0e0}
textarea{width:100%;height:120px;padding:12px;background:#1a1a1a;color:#fff;border:1px solid #333;border-radius:8px;font-size:16px}
button{margin-top:12px;padding:12px 24px;background:#7c3aed;color:#fff;border:none;border-radius:8px;font-size:16px;cursor:pointer}
button:hover{background:#6d28d9}
pre{background:#161616;padding:16px;border-radius:8px;white-space:pre-wrap;margin-top:16px;line-height:1.5}
</style></head><body>
<h2>🟣 Hermes + NVIDIA NIM</h2>
<textarea id="p" placeholder="Type your prompt..."></textarea><br>
<button onclick="run()">Run Hermes</button>
<pre id="out">Response will appear here...</pre>
<script>
async function run(){
const o=document.getElementById('out'); o.textContent='⏳ Running...';
try{
const r=await fetch('/run',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({prompt:document.getElementById('p').value})});
const d=await r.json(); o.textContent=d.output||d.detail||JSON.stringify(d);
}catch(e){o.textContent='❌ '+e.message}
}
</script></body></html>
"""
@app.post("/run")
async def run_hermes(req: Request):
try:
data = await req.json()
prompt = data.get("prompt", "")
if not prompt:
return {"error": "Missing prompt"}
client = get_client()
res = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
max_tokens=1024
)
return {"status": "done", "output": res.choices[0].message.content}
except Exception as e:
return {"status": "error", "detail": str(e)}