Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,65 +5,106 @@ import uuid
|
|
| 5 |
import os
|
| 6 |
import tempfile
|
| 7 |
import pymongo
|
|
|
|
| 8 |
import certifi
|
| 9 |
from bson.objectid import ObjectId
|
| 10 |
|
| 11 |
-
# --- 1. DB ์ค์ ---
|
| 12 |
MONGO_URI = os.environ.get("MONGO_URI")
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
print(f"โ MongoDB ์ฐ๊ฒฐ ์คํจ: {e}")
|
| 28 |
-
# ๋ฐฐํฌ ์ ๋ก๊ทธ ํ์ธ์ ์ํด ์๋ฌ ์ถ๋ ฅ
|
| 29 |
-
import traceback
|
| 30 |
-
traceback.print_exc()
|
| 31 |
-
|
| 32 |
-
db = client.story_assistant_db
|
| 33 |
-
interactions_col = db.interactions
|
| 34 |
-
summaries_col = db.summaries
|
| 35 |
-
|
| 36 |
-
# ... (๋๋จธ์ง ์ฝ๋๋ ๊ธฐ์กด๊ณผ ์๋ฒฝํ ๋์ผํฉ๋๋ค) ...
|
| 37 |
-
# ์๋๋ ๋ณต์ฌ ๋ถ์ฌ๋ฃ๊ธฐ ํธ์๋ฅผ ์ํด ์ ์ฒด ์ฝ๋๋ฅผ ์ ์งํฉ๋๋ค.
|
| 38 |
-
|
| 39 |
-
# --- 2. DB ํจ์๋ค ---
|
| 40 |
def log_interaction(session_id, user_id, role, content):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def log_summary(session_id, user_id, summary_text):
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def fetch_history(session_id):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# --- 3. LLM API ์ค์ ---
|
| 69 |
api_key = os.environ.get("GEMINI_API_KEY")
|
|
@@ -71,9 +112,7 @@ if not api_key:
|
|
| 71 |
try:
|
| 72 |
with open("api_key.txt", "r") as f: api_key = f.read().strip()
|
| 73 |
except: pass
|
| 74 |
-
|
| 75 |
-
if not api_key:
|
| 76 |
-
print("โ ๏ธ GEMINI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
| 77 |
|
| 78 |
genai.configure(api_key=api_key)
|
| 79 |
|
|
@@ -81,7 +120,7 @@ try:
|
|
| 81 |
with open("system_prompt.txt", "r", encoding="utf-8") as f: SYSTEM_PROMPT = f.read()
|
| 82 |
except: SYSTEM_PROMPT = "๋น์ ์ ์น์ ํ ์ฑ๋ด์
๋๋ค."
|
| 83 |
|
| 84 |
-
# ๋ชจ๋ธ๋ช
์ฌํ์ธ (gemini-1.5-flash
|
| 85 |
model = genai.GenerativeModel(model_name='gemini-1.5-flash', system_instruction=SYSTEM_PROMPT)
|
| 86 |
|
| 87 |
WELCOME_MESSAGE = (
|
|
@@ -184,6 +223,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 184 |
|
| 185 |
with gr.Group(visible=False) as chat_view:
|
| 186 |
gr.Markdown("# ๐ ์ด์ผ๊ธฐ ๋น์ (ํ๋กํ ํ์
)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
with gr.Row():
|
| 188 |
with gr.Column(scale=3):
|
| 189 |
chatbot = gr.Chatbot(
|
|
|
|
| 5 |
import os
|
| 6 |
import tempfile
|
| 7 |
import pymongo
|
| 8 |
+
import sqlite3
|
| 9 |
import certifi
|
| 10 |
from bson.objectid import ObjectId
|
| 11 |
|
| 12 |
+
# --- 1. ํ์ด๋ธ๋ฆฌ๋ DB ์ค์ ---
|
| 13 |
MONGO_URI = os.environ.get("MONGO_URI")
|
| 14 |
+
USE_MONGO = False # ๊ธฐ๋ณธ๊ฐ์ False, ์ฐ๊ฒฐ ์ฑ๊ณต ์ True๋ก ๋ณ๊ฒฝ
|
| 15 |
|
| 16 |
+
# 1-1. MongoDB ์ฐ๊ฒฐ ์๋
|
| 17 |
+
if MONGO_URI:
|
| 18 |
+
try:
|
| 19 |
+
print("๐ MongoDB ์ฐ๊ฒฐ ์๋ ์ค...")
|
| 20 |
+
client = pymongo.MongoClient(
|
| 21 |
+
MONGO_URI,
|
| 22 |
+
serverSelectionTimeoutMS=3000, # 3์ด ๋ด ์๋ต ์์ผ๋ฉด ํฌ๊ธฐ
|
| 23 |
+
tls=True,
|
| 24 |
+
tlsAllowInvalidCertificates=True,
|
| 25 |
+
tlsAllowInvalidHostnames=True
|
| 26 |
+
)
|
| 27 |
+
client.admin.command('ping')
|
| 28 |
+
USE_MONGO = True
|
| 29 |
+
print("โ
MongoDB ์ฐ๊ฒฐ ์ฑ๊ณต! (์๊ฒฉ ์ ์ฅ์ ์ฌ์ฉ)")
|
| 30 |
+
db = client.story_assistant_db
|
| 31 |
+
interactions_col = db.interactions
|
| 32 |
+
summaries_col = db.summaries
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"โ ๏ธ MongoDB ์ฐ๊ฒฐ ์คํจ: {e}")
|
| 35 |
+
print("๐ SQLite ๋ก์ปฌ ์ ์ฅ์๋ก ์ ํํฉ๋๋ค.")
|
| 36 |
+
USE_MONGO = False
|
| 37 |
+
else:
|
| 38 |
+
print("โน๏ธ MONGO_URI๊ฐ ์์ต๋๋ค. SQLite ๋ก์ปฌ ์ ์ฅ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.")
|
| 39 |
+
|
| 40 |
+
# 1-2. SQLite ์ค์ (MongoDB ์คํจ ์ ๋ฐฑ์
์ฉ)
|
| 41 |
+
if not USE_MONGO:
|
| 42 |
+
# Hugging Face Spaces์ ์๊ตฌ ์ ์ฅ์ ๊ฒฝ๋ก(/data)๊ฐ ์์ผ๋ฉด ๊ฑฐ๊ธฐ ์ ์ฅ, ์์ผ๋ฉด ์ผ๋ฐ ํ์ผ
|
| 43 |
+
DB_PATH = os.path.join("/data", "logs.db") if os.path.exists("/data") else "logs.db"
|
| 44 |
+
print(f"๐ SQLite ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฒฝ๋ก: {DB_PATH}")
|
| 45 |
+
|
| 46 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 47 |
+
cursor = conn.cursor()
|
| 48 |
+
cursor.execute('''
|
| 49 |
+
CREATE TABLE IF NOT EXISTS interactions (
|
| 50 |
+
session_id TEXT, user_id TEXT, timestamp TEXT, role TEXT, content TEXT
|
| 51 |
+
)
|
| 52 |
+
''')
|
| 53 |
+
cursor.execute('''
|
| 54 |
+
CREATE TABLE IF NOT EXISTS summaries (
|
| 55 |
+
session_id TEXT, user_id TEXT, timestamp TEXT, summary TEXT
|
| 56 |
)
|
| 57 |
+
''')
|
| 58 |
+
conn.commit()
|
| 59 |
+
|
| 60 |
+
# --- 2. ํตํฉ DB ํจ์๋ค (ํ์ด๋ธ๋ฆฌ๋) ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def log_interaction(session_id, user_id, role, content):
|
| 62 |
+
timestamp = datetime.datetime.now().isoformat()
|
| 63 |
+
if USE_MONGO:
|
| 64 |
+
try:
|
| 65 |
+
interactions_col.insert_one({
|
| 66 |
+
"session_id": session_id, "user_id": user_id,
|
| 67 |
+
"timestamp": timestamp, "role": role, "content": content
|
| 68 |
+
})
|
| 69 |
+
except Exception as e: print(f"DB Log Error (Mongo): {e}")
|
| 70 |
+
else:
|
| 71 |
+
try:
|
| 72 |
+
cursor.execute("INSERT INTO interactions VALUES (?, ?, ?, ?, ?)",
|
| 73 |
+
(session_id, user_id, timestamp, role, content))
|
| 74 |
+
conn.commit()
|
| 75 |
+
except Exception as e: print(f"DB Log Error (SQLite): {e}")
|
| 76 |
|
| 77 |
def log_summary(session_id, user_id, summary_text):
|
| 78 |
+
timestamp = datetime.datetime.now().isoformat()
|
| 79 |
+
if USE_MONGO:
|
| 80 |
+
try:
|
| 81 |
+
summaries_col.insert_one({
|
| 82 |
+
"session_id": session_id, "user_id": user_id,
|
| 83 |
+
"timestamp": timestamp, "summary": summary_text
|
| 84 |
+
})
|
| 85 |
+
except Exception as e: print(f"DB Summary Error (Mongo): {e}")
|
| 86 |
+
else:
|
| 87 |
+
try:
|
| 88 |
+
cursor.execute("INSERT INTO summaries VALUES (?, ?, ?, ?)",
|
| 89 |
+
(session_id, user_id, timestamp, summary_text))
|
| 90 |
+
conn.commit()
|
| 91 |
+
except Exception as e: print(f"DB Summary Error (SQLite): {e}")
|
| 92 |
|
| 93 |
def fetch_history(session_id):
|
| 94 |
+
if USE_MONGO:
|
| 95 |
+
try:
|
| 96 |
+
cursor_mongo = interactions_col.find({"session_id": session_id}).sort("timestamp", 1)
|
| 97 |
+
return [(doc["role"], doc["content"], doc["timestamp"]) for doc in cursor_mongo]
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"DB Fetch Error (Mongo): {e}")
|
| 100 |
+
return []
|
| 101 |
+
else:
|
| 102 |
+
try:
|
| 103 |
+
cursor.execute("SELECT role, content, timestamp FROM interactions WHERE session_id=? ORDER BY timestamp", (session_id,))
|
| 104 |
+
return cursor.fetchall()
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"DB Fetch Error (SQLite): {e}")
|
| 107 |
+
return []
|
| 108 |
|
| 109 |
# --- 3. LLM API ์ค์ ---
|
| 110 |
api_key = os.environ.get("GEMINI_API_KEY")
|
|
|
|
| 112 |
try:
|
| 113 |
with open("api_key.txt", "r") as f: api_key = f.read().strip()
|
| 114 |
except: pass
|
| 115 |
+
if not api_key: print("โ ๏ธ GEMINI_API_KEY๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
|
|
|
|
|
|
| 116 |
|
| 117 |
genai.configure(api_key=api_key)
|
| 118 |
|
|
|
|
| 120 |
with open("system_prompt.txt", "r", encoding="utf-8") as f: SYSTEM_PROMPT = f.read()
|
| 121 |
except: SYSTEM_PROMPT = "๋น์ ์ ์น์ ํ ์ฑ๋ด์
๋๋ค."
|
| 122 |
|
| 123 |
+
# ๋ชจ๋ธ๋ช
์ฌํ์ธ (gemini-1.5-flash)
|
| 124 |
model = genai.GenerativeModel(model_name='gemini-1.5-flash', system_instruction=SYSTEM_PROMPT)
|
| 125 |
|
| 126 |
WELCOME_MESSAGE = (
|
|
|
|
| 223 |
|
| 224 |
with gr.Group(visible=False) as chat_view:
|
| 225 |
gr.Markdown("# ๐ ์ด์ผ๊ธฐ ๋น์ (ํ๋กํ ํ์
)")
|
| 226 |
+
|
| 227 |
+
# DB ์ํ ํ์ (๋๋ฒ๊น
์ฉ)
|
| 228 |
+
db_status = "๐ข MongoDB ์ฐ๊ฒฐ๋จ" if USE_MONGO else "๐ SQLite ๋ก์ปฌ ๋ชจ๋ (์ฃผ์: ์ฌ์์ ์ ๋ฐ์ดํฐ ์ด๊ธฐํ ๊ฐ๋ฅ)"
|
| 229 |
+
gr.Markdown(f"โน๏ธ ์์คํ
์ํ: {db_status}")
|
| 230 |
+
|
| 231 |
with gr.Row():
|
| 232 |
with gr.Column(scale=3):
|
| 233 |
chatbot = gr.Chatbot(
|