YSKang commited on
Commit
2b01c20
ยท
verified ยท
1 Parent(s): a802c7b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
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 ์ธ์ฆ์„œ ๊ฒ€์ฆ ๋น„ํ™œ์„ฑํ™” (Hugging Face Spaces ํ™˜๊ฒฝ ๋ฌธ์ œ ํ•ด๊ฒฐ์šฉ)
 
16
  client = pymongo.MongoClient(
17
  MONGO_URI,
18
  serverSelectionTimeoutMS=5000,
19
- tlsAllowInvalidCertificates=True
 
20
  )
21
- client.server_info()
 
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-2.5-flash', system_instruction=SYSTEM_PROMPT)
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": ""}) # ๋ด‡ ์‘๋‹ต placeholder ์ถ”๊ฐ€
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", # ์ตœ์‹  Gradio ํฌ๋งท
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():