Spaces:
Runtime error
Runtime error
from fastapi import FastAPI | |
from fastapi.responses import HTMLResponse | |
import logging | |
# Set up logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
# Initialize FastAPI app | |
app = FastAPI() | |
# Serve HTML page | |
async def serve_html(): | |
logger.info("Serving HTML page") | |
return """ | |
<html> | |
<head> | |
<title>AI Chemistry Chat</title> | |
<style> | |
body { font-family: Arial, sans-serif; margin: 20px; } | |
#response { white-space: pre-wrap; border: 1px solid #ccc; padding: 10px; min-height: 100px; } | |
input { width: 300px; padding: 5px; } | |
button { padding: 5px 10px; } | |
</style> | |
</head> | |
<body> | |
<h1>AI Chemistry Chat</h1> | |
<input type="text" id="prompt" placeholder="Ask about chemistry..." value="Tell me something I might not know about chemistry." /> | |
<button onclick="sendPrompt()">Send</button> | |
<div id="response"></div> | |
<script src="https://js.puter.com/v2/"></script> | |
<script> | |
async function sendPrompt() { | |
const prompt = document.getElementById('prompt').value; | |
const responseDiv = document.getElementById('response'); | |
responseDiv.innerHTML = 'Loading...'; | |
try { | |
const resp = await puter.ai.chat(prompt, { model: 'grok-beta', stream: true }); | |
responseDiv.innerHTML = ''; | |
for await (const part of resp) { | |
responseDiv.innerHTML += part?.text?.replaceAll('\\n', '<br>') || ''; | |
} | |
} catch (error) { | |
responseDiv.innerHTML = 'Error: ' + error.message; | |
} | |
} | |
</script> | |
</body> | |
</html> | |
""" | |
# Health check endpoint | |
async def health(): | |
logger.info("Health check requested") | |
return {"status": "healthy"} | |