from flask import Flask, request, session, render_template_string, jsonify
import requests
import json
app = Flask(__name__)
app.secret_key = "retro_terminal_ai_chat_2026" # required for sessions
API_URL = "https://corvo-ai-xx-claude-4-5.hf.space/chat"
HTML_PAGE = """
AI Terminal Chat
"""
# Serve main page
@app.route("/")
def index():
return render_template_string(HTML_PAGE)
# Get chat history for session
@app.route("/history")
def history():
chat_history = session.get("chat_history", [])
return jsonify(chat_history)
# Chat API
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json.get("message")
if not user_message:
return jsonify({"reply": "No message received."})
# Initialize session chat if not exists
if "chat_history" not in session:
session["chat_history"] = []
chat_history = session["chat_history"]
# Append user message
chat_history.append({"role": "user", "content": user_message})
payload = {
"chat_history": chat_history,
"temperature": 0.1,
"top_p": 0.95,
"max_tokens": 2000
}
try:
response = requests.post(API_URL, json=payload, timeout=120)
response.raise_for_status()
assistant_reply = response.json().get("assistant_response", "No response.")
except Exception as e:
assistant_reply = "ERROR: " + str(e)
# Append AI reply
chat_history.append({"role": "assistant", "content": assistant_reply})
# Save back to session
session["chat_history"] = chat_history
return jsonify({"reply": assistant_reply})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)