mcmeszi commited on
Commit
a13453f
·
verified ·
1 Parent(s): 338ce42

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +12 -24
server.js CHANGED
@@ -6,40 +6,32 @@ import dotenv from "dotenv";
6
  dotenv.config();
7
 
8
  const app = express();
9
- const PORT = process.env.PORT || 7860; // Hugging Face Space default port
 
10
 
11
- // ─────────────────────────────────────────────────────────────
12
- // Ellenőrizzük, hogy van‑e OpenAI‑kulcs
13
- // ─────────────────────────────────────────────────────────────
14
  if (!process.env.OPENAI_KEY) {
15
  console.error("❌ Hiányzik az OPENAI_KEY környezeti változó!");
16
  process.exit(1);
17
  }
18
 
19
- // ─────────────────────────────────────────────────────────────
20
- // Middleware
21
- // ─────────────────────────────────────────────────────────────
22
  app.use(express.json());
23
- app.use(express.static("public")); // kiszolgáljuk a frontend fájlokat
24
 
25
- // ─────────────────────────────────────────────────────────────
26
- // POST /api/analyze – Biblia‑vers + AI‑elemzés
27
- // Body: { book, chapter, verse, translation }
28
- // ─────────────────────────────────────────────────────────────
29
  app.post("/api/analyze", async (req, res) => {
30
  const { book, chapter, verse, translation } = req.body || {};
31
  const ref = `${book} ${chapter}:${verse}`;
 
32
 
33
  try {
34
- // 1) Vers lekérése a Bible‑API‑ról
35
  const bibRes = await fetch(
36
- `https://bible-api.com/${encodeURIComponent(ref)}?translation=${translation}`
37
  );
38
  if (!bibRes.ok) throw new Error("Bible‑API hiba");
39
  const bibData = await bibRes.json();
40
  const verseText = bibData.text.trim();
41
 
42
- // 2) AI‑elemzés a GPT‑4o-mini modellel
43
  const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
44
  method: "POST",
45
  headers: {
@@ -52,15 +44,15 @@ app.post("/api/analyze", async (req, res) => {
52
  {
53
  role: "system",
54
  content:
55
- "You are the narrator of Theos ex Machina, analysing Bible verses from theological and AI‑ethical perspectives."
56
  },
57
  {
58
  role: "user",
59
- content: `Here is the verse: "${verseText}" — ${ref} (${translation.toUpperCase()}). Analyse how it resonates with the philosophical challenges and ethical dilemmas of artificial intelligence.`
60
  }
61
  ],
62
- temperature: 0.8,
63
- max_tokens: 512
64
  })
65
  });
66
 
@@ -68,9 +60,8 @@ app.post("/api/analyze", async (req, res) => {
68
  const aiData = await aiRes.json();
69
  const analysis = aiData.choices[0].message.content.trim();
70
 
71
- // 3) Válasz
72
  res.json({
73
- reference: `${ref} (${translation.toUpperCase()})`,
74
  verseText,
75
  analysis
76
  });
@@ -80,9 +71,6 @@ app.post("/api/analyze", async (req, res) => {
80
  }
81
  });
82
 
83
- // ─────────────────────────────────────────────────────────────
84
- // Szerver indítása
85
- // ─────────────────────────────────────────────────────────────
86
  app.listen(PORT, () =>
87
  console.log(`🚀 Space‑szerver fut a(z) ${PORT} porton`)
88
  );
 
6
  dotenv.config();
7
 
8
  const app = express();
9
+ const PORT = process.env.PORT || 7860;
10
+ const DEFAULT_TRANSLATION = "karoli"; // Károli fordítás
11
 
 
 
 
12
  if (!process.env.OPENAI_KEY) {
13
  console.error("❌ Hiányzik az OPENAI_KEY környezeti változó!");
14
  process.exit(1);
15
  }
16
 
 
 
 
17
  app.use(express.json());
18
+ app.use(express.static("public"));
19
 
 
 
 
 
20
  app.post("/api/analyze", async (req, res) => {
21
  const { book, chapter, verse, translation } = req.body || {};
22
  const ref = `${book} ${chapter}:${verse}`;
23
+ const trans = translation || DEFAULT_TRANSLATION;
24
 
25
  try {
26
+ // 1) Vers lekérése
27
  const bibRes = await fetch(
28
+ `https://bible-api.com/${encodeURIComponent(ref)}?translation=${trans}`
29
  );
30
  if (!bibRes.ok) throw new Error("Bible‑API hiba");
31
  const bibData = await bibRes.json();
32
  const verseText = bibData.text.trim();
33
 
34
+ // 2) AI‑elemzés (magyar, 1. személy, max 2 bekezdés)
35
  const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
36
  method: "POST",
37
  headers: {
 
44
  {
45
  role: "system",
46
  content:
47
+ "Te vagy a Theos ex Machina narrátora. Magyarul, első szám első személyben, legfeljebb két tömör bekezdésben, kreatívan magyarázd el a bibliai vers jelentését, és kapcsold össze a modern technológia vagy mesterséges intelligencia dilemmáival."
48
  },
49
  {
50
  role: "user",
51
+ content: `Íme a vers: ${verseText} — ${ref} (Károli). Kérlek, írd meg az elemzést a fenti stílusban.`
52
  }
53
  ],
54
+ temperature: 0.9,
55
+ max_tokens: 256
56
  })
57
  });
58
 
 
60
  const aiData = await aiRes.json();
61
  const analysis = aiData.choices[0].message.content.trim();
62
 
 
63
  res.json({
64
+ reference: `${ref} (Károli)`,
65
  verseText,
66
  analysis
67
  });
 
71
  }
72
  });
73
 
 
 
 
74
  app.listen(PORT, () =>
75
  console.log(`🚀 Space‑szerver fut a(z) ${PORT} porton`)
76
  );