Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -12,21 +12,30 @@ from bson.objectid import ObjectId
|
|
| 12 |
MONGO_URI = os.environ.get("MONGO_URI")
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
# ๐จ ์์ : SSL
|
|
|
|
| 16 |
client = pymongo.MongoClient(
|
| 17 |
MONGO_URI,
|
| 18 |
serverSelectionTimeoutMS=5000,
|
| 19 |
-
|
|
|
|
| 20 |
)
|
| 21 |
-
|
|
|
|
| 22 |
print("โ
MongoDB ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 23 |
except Exception as e:
|
| 24 |
print(f"โ MongoDB ์ฐ๊ฒฐ ์คํจ: {e}")
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
db = client.story_assistant_db
|
| 27 |
interactions_col = db.interactions
|
| 28 |
summaries_col = db.summaries
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
# --- 2. DB ํจ์๋ค ---
|
| 31 |
def log_interaction(session_id, user_id, role, content):
|
| 32 |
try:
|
|
@@ -72,7 +81,7 @@ try:
|
|
| 72 |
with open("system_prompt.txt", "r", encoding="utf-8") as f: SYSTEM_PROMPT = f.read()
|
| 73 |
except: SYSTEM_PROMPT = "๋น์ ์ ์น์ ํ ์ฑ๋ด์
๋๋ค."
|
| 74 |
|
| 75 |
-
model = genai.GenerativeModel(model_name='gemini-
|
| 76 |
|
| 77 |
WELCOME_MESSAGE = (
|
| 78 |
"์๋
ํ์ธ์, ์ ์๋. '์ด์ผ๊ธฐ ๋น์'์
๋๋ค.\n"
|
|
@@ -85,7 +94,6 @@ WELCOME_MESSAGE = (
|
|
| 85 |
def format_history_for_llm(history):
|
| 86 |
llm_history = []
|
| 87 |
for msg in history:
|
| 88 |
-
# Gradio ์ต์ ๋ฒ์ ์ 'messages' ํฌ๋งท์ ๋ง์ถฐ ์์
|
| 89 |
if msg['role'] == 'user':
|
| 90 |
llm_history.append({"role": "user", "parts": [{"text": msg['content']}]})
|
| 91 |
elif msg['role'] == 'assistant':
|
|
@@ -95,7 +103,6 @@ def format_history_for_llm(history):
|
|
| 95 |
def chat(user_input, history, session_id, user_id):
|
| 96 |
if not user_input.strip(): return "", history
|
| 97 |
|
| 98 |
-
# Gradio ์ต์ 'messages' ํฌ๋งท ์ฌ์ฉ
|
| 99 |
history.append({"role": "user", "content": user_input})
|
| 100 |
log_interaction(session_id, user_id, 'user', user_input)
|
| 101 |
yield "", history
|
|
@@ -105,7 +112,7 @@ def chat(user_input, history, session_id, user_id):
|
|
| 105 |
try:
|
| 106 |
response = chat_session.send_message(user_input, stream=True)
|
| 107 |
full_response = ""
|
| 108 |
-
history.append({"role": "assistant", "content": ""})
|
| 109 |
for chunk in response:
|
| 110 |
full_response += chunk.text
|
| 111 |
history[-1]['content'] = full_response
|
|
@@ -128,8 +135,6 @@ def change_topic(history, session_id, user_id):
|
|
| 128 |
summary_text = response.text
|
| 129 |
log_summary(session_id, user_id, summary_text)
|
| 130 |
end_message = f"\n\n[๊ธฐ๋ก ์๋ฃ] ์ง๊ธ๊น์ง์ ์ด์ผ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ธฐ๋กํด ๋์์ต๋๋ค.\n\n๐ **์์ฝ:** {summary_text}\n\n์, ๊ทธ๋ผ ์ด์ ๋ค๋ฅธ ์ฃผ์ ๋ก ๋์ด๊ฐ ๋ณผ๊น์? ์ด๋ค ์ด์ผ๊ธฐ๋ฅผ ๋ ๋ค๋ ค์ฃผ์๊ฒ ์ด์?"
|
| 131 |
-
|
| 132 |
-
# ์์คํ
๋ฉ์์ง์ฒ๋ผ ๋ณด์ด๊ฒ assistant ์ญํ ๋ก ์ถ๊ฐ
|
| 133 |
history.append({"role": "assistant", "content": end_message})
|
| 134 |
return history
|
| 135 |
except Exception as e:
|
|
@@ -180,12 +185,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 180 |
gr.Markdown("# ๐ ์ด์ผ๊ธฐ ๋น์ (ํ๋กํ ํ์
)")
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column(scale=3):
|
| 183 |
-
# ๐จ ์์ : type='messages'๋ก ๋ณ๊ฒฝํ์ฌ Gradio ๊ฒฝ๊ณ ํด๊ฒฐ
|
| 184 |
chatbot = gr.Chatbot(
|
| 185 |
value=[{"role": "assistant", "content": WELCOME_MESSAGE}],
|
| 186 |
height=650,
|
| 187 |
label="๋ํ์ฐฝ",
|
| 188 |
-
type="messages",
|
| 189 |
avatar_images=(None, "https://i.ibb.co/ZHrkBPm/ai-assistant.png")
|
| 190 |
)
|
| 191 |
with gr.Row():
|
|
|
|
| 12 |
MONGO_URI = os.environ.get("MONGO_URI")
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# ๐จ ์์ : SSL/TLS๋ฅผ ๊ฐ์ ๋ก ๋นํ์ฑํํ์ฌ ์ฐ๊ฒฐ ์๋
|
| 16 |
+
# (์ฃผ์: ํ๋กํ ํ์
์ฉ ์ค์ ์
๋๋ค. ์ค์ ์๋น์ค์์๋ ๋ณด์ ์ํ์ด ์์ต๋๋ค.)
|
| 17 |
client = pymongo.MongoClient(
|
| 18 |
MONGO_URI,
|
| 19 |
serverSelectionTimeoutMS=5000,
|
| 20 |
+
tls=False, # TLS ์ํธํ ๋นํ์ฑํ (๊ฐ์ฅ ๊ฐ๋ ฅํ ํด๊ฒฐ์ฑ
)
|
| 21 |
+
tlsAllowInvalidCertificates=True # ํน์ TLS๊ฐ ๊ฐ์ ๋ ๊ฒฝ์ฐ ์ธ์ฆ์ ๋ฌด์
|
| 22 |
)
|
| 23 |
+
# ์ฐ๊ฒฐ ํ
์คํธ ๋ช
๋ น ์คํ
|
| 24 |
+
client.admin.command('ping')
|
| 25 |
print("โ
MongoDB ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
| 26 |
except Exception as e:
|
| 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 |
try:
|
|
|
|
| 81 |
with open("system_prompt.txt", "r", encoding="utf-8") as f: SYSTEM_PROMPT = f.read()
|
| 82 |
except: SYSTEM_PROMPT = "๋น์ ์ ์น์ ํ ์ฑ๋ด์
๋๋ค."
|
| 83 |
|
| 84 |
+
model = genai.GenerativeModel(model_name='gemini-1.5-flash', system_instruction=SYSTEM_PROMPT)
|
| 85 |
|
| 86 |
WELCOME_MESSAGE = (
|
| 87 |
"์๋
ํ์ธ์, ์ ์๋. '์ด์ผ๊ธฐ ๋น์'์
๋๋ค.\n"
|
|
|
|
| 94 |
def format_history_for_llm(history):
|
| 95 |
llm_history = []
|
| 96 |
for msg in history:
|
|
|
|
| 97 |
if msg['role'] == 'user':
|
| 98 |
llm_history.append({"role": "user", "parts": [{"text": msg['content']}]})
|
| 99 |
elif msg['role'] == 'assistant':
|
|
|
|
| 103 |
def chat(user_input, history, session_id, user_id):
|
| 104 |
if not user_input.strip(): return "", history
|
| 105 |
|
|
|
|
| 106 |
history.append({"role": "user", "content": user_input})
|
| 107 |
log_interaction(session_id, user_id, 'user', user_input)
|
| 108 |
yield "", history
|
|
|
|
| 112 |
try:
|
| 113 |
response = chat_session.send_message(user_input, stream=True)
|
| 114 |
full_response = ""
|
| 115 |
+
history.append({"role": "assistant", "content": ""})
|
| 116 |
for chunk in response:
|
| 117 |
full_response += chunk.text
|
| 118 |
history[-1]['content'] = full_response
|
|
|
|
| 135 |
summary_text = response.text
|
| 136 |
log_summary(session_id, user_id, summary_text)
|
| 137 |
end_message = f"\n\n[๊ธฐ๋ก ์๋ฃ] ์ง๊ธ๊น์ง์ ์ด์ผ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ธฐ๋กํด ๋์์ต๋๋ค.\n\n๐ **์์ฝ:** {summary_text}\n\n์, ๊ทธ๋ผ ์ด์ ๋ค๋ฅธ ์ฃผ์ ๋ก ๋์ด๊ฐ ๋ณผ๊น์? ์ด๋ค ์ด์ผ๊ธฐ๋ฅผ ๋ ๋ค๋ ค์ฃผ์๊ฒ ์ด์?"
|
|
|
|
|
|
|
| 138 |
history.append({"role": "assistant", "content": end_message})
|
| 139 |
return history
|
| 140 |
except Exception as e:
|
|
|
|
| 185 |
gr.Markdown("# ๐ ์ด์ผ๊ธฐ ๋น์ (ํ๋กํ ํ์
)")
|
| 186 |
with gr.Row():
|
| 187 |
with gr.Column(scale=3):
|
|
|
|
| 188 |
chatbot = gr.Chatbot(
|
| 189 |
value=[{"role": "assistant", "content": WELCOME_MESSAGE}],
|
| 190 |
height=650,
|
| 191 |
label="๋ํ์ฐฝ",
|
| 192 |
+
type="messages",
|
| 193 |
avatar_images=(None, "https://i.ibb.co/ZHrkBPm/ai-assistant.png")
|
| 194 |
)
|
| 195 |
with gr.Row():
|