first commit

This commit is contained in:
2026-01-09 07:37:56 -03:00
parent f28a9f79f4
commit 8483d9b8e9
4 changed files with 250 additions and 0 deletions

67
files/app.py Normal file
View File

@@ -0,0 +1,67 @@
from flask import Flask, render_template, request, jsonify
import traceback
import json
# 🔥 IMPORTA SEU PIPELINE
from oci_genai_llm_graphrag_financial import answer_question
app = Flask(__name__)
def parse_llm_json(raw: str) -> dict:
try:
raw = raw.replace("```json", "")
raw = raw.replace("```", "")
return json.loads(raw)
except Exception:
return {
"answer": "ERROR",
"justification": "LLM returned invalid JSON",
"raw_output": raw
}
# =========================
# Health check (Load Balancer)
# =========================
@app.route("/health", methods=["GET"])
def health():
return jsonify({"status": "UP"}), 200
# =========================
# Página Web
# =========================
@app.route("/", methods=["GET"])
def index():
return render_template("index.html")
# =========================
# Endpoint de Chat
# =========================
@app.route("/chat", methods=["POST"])
def chat():
try:
data = request.get_json()
question = data.get("question", "").strip()
if not question:
return jsonify({"error": "Empty question"}), 400
raw_answer = answer_question(question)
parsed_answer = parse_llm_json(raw_answer)
return jsonify({
"question": question,
"result": parsed_answer
})
except Exception as e:
traceback.print_exc()
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=8100,
debug=False
)

98
files/index.html Normal file
View File

@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GraphRAG Chat</title>
<style>
body {
background: linear-gradient(to bottom right, #0f172a, #1e293b);
min-height: 100vh;
color: #e2e8f0;
}
pre { white-space: pre-wrap; }
</style>
<style>
body {
font-family: Arial, sans-serif;
background: #0f172a;
color: #e5e7eb;
padding: 30px;
}
textarea {
width: 100%;
height: 80px;
font-size: 16px;
}
button {
margin-top: 10px;
padding: 10px 20px;
font-size: 16px;
}
pre {
background: #020617;
padding: 20px;
white-space: pre-wrap;
border-radius: 8px;
}
</style>
</head>
<body>
<h1>🧠 Oracle GraphRAG Chat</h1>
<div class="mt-12 mb-8 text-center">
<div class="inline-block bg-slate-800/40 px-6 py-4 rounded-2xl shadow-lg border border-slate-700">
<p class="text-slate-400 text-sm">
Oracle LAD A-Team<br/>
<span class="text-blue-300 font-semibold">Cristiano Hoshikawa</span><br/>
<span class="text-blue-300 font-semibold">cristiano.hoshikawa@oracle.com</span>
</p>
<p class="text-slate-500 text-xs mt-1 italic">
<span class="text-blue-300 font-semibold">Tutorial in: https://docs.oracle.com/en/learn/oci-genai-pdf</span><br/>
</p>
<p class="text-slate-500 text-xs mt-1 italic">
GraphRAG • Oracle 23ai • Embeddings • LLM • Flask API
</p>
<div class="mt-2 text-slate-500 text-xs flex items-center justify-center gap-1">
<span>Demo Version</span>
</div>
</div>
</div>
<textarea id="question" placeholder="Ask a question..."></textarea>
<br>
<button onclick="send()">Ask</button>
<h2>Answer</h2>
<pre id="answer"></pre>
<script>
async function send() {
const question = document.getElementById("question").value;
const answerBox = document.getElementById("answer");
answerBox.textContent = "⏳ Thinking...";
const res = await fetch("/chat", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({question})
});
const data = await res.json();
if (data.result && data.result.answer) {
answerBox.textContent = JSON.stringify(data.result, null, 2);
} else {
answerBox.textContent = "❌ Error: " + JSON.stringify(data);
}
}
</script>
</body>
</html>