Euryeth's picture
Update app.py
3da9c3c verified
Raw
History Blame Contribute Delete
2.79 kB
from flask import Flask, render_template_string
from api import api # your OpenAI-style API blueprint
app = Flask(__name__)
app.register_blueprint(api)
@app.route("/")
def index():
return render_template_string("""
<!doctype html>
<html>
<head>
<title>LLM Chat</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
textarea { width: 100%; max-width: 600px; }
#chat-output p { max-width: 600px; padding: 5px 10px; border-radius: 5px; }
#chat-output p.user { background-color: #d1e7dd; text-align: left; }
#chat-output p.assistant { background-color: #f8d7da; text-align: left; }
</style>
</head>
<body>
<h1>Chat with Hermes-7B</h1>
<form id="chat-form">
<textarea id="user-input" rows="4" placeholder="Say something..."></textarea><br>
<button type="submit">Send</button>
</form>
<div id="chat-output"></div>
<script>
const form = document.getElementById('chat-form');
const input = document.getElementById('user-input');
const output = document.getElementById('chat-output');
form.onsubmit = async (e) => {
e.preventDefault();
const userMessage = input.value.trim();
if (!userMessage) return;
output.innerHTML += `<p class="user"><strong>You:</strong> ${userMessage}</p>`;
input.value = "";
try {
const response = await fetch('/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [{ role: "user", content: userMessage }],
stream: false
})
});
if (!response.ok) {
const error = await response.json().catch(() => ({}));
output.innerHTML += `<p class="assistant"><strong>Error:</strong> ${error.error || response.statusText}</p>`;
return;
}
const data = await response.json();
const message = data.choices?.[0]?.message?.content || "[No response]";
output.innerHTML += `<p class="assistant"><strong>Assistant:</strong> ${message}</p>`;
} catch (err) {
output.innerHTML += `<p class="assistant"><strong>Error:</strong> ${err.message}</p>`;
}
};
</script>
</body>
</html>
""")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)