Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,11 @@ from bson.objectid import ObjectId
|
|
| 12 |
MONGO_URI = os.environ.get("MONGO_URI")
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
client = pymongo.MongoClient(
|
| 17 |
MONGO_URI,
|
| 18 |
serverSelectionTimeoutMS=5000,
|
| 19 |
-
|
| 20 |
)
|
| 21 |
client.server_info()
|
| 22 |
print("โ
MongoDB ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
|
@@ -59,7 +59,6 @@ def fetch_history(session_id):
|
|
| 59 |
# --- 3. LLM API ์ค์ ---
|
| 60 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 61 |
if not api_key:
|
| 62 |
-
# ๋ก์ปฌ ํ
์คํธ์ฉ (๋ฐฐํฌ ์์๋ ์ด ๋ถ๋ถ์ด ์คํ๋์ง ์์์ผ ์ ์)
|
| 63 |
try:
|
| 64 |
with open("api_key.txt", "r") as f: api_key = f.read().strip()
|
| 65 |
except: pass
|
|
@@ -85,32 +84,39 @@ WELCOME_MESSAGE = (
|
|
| 85 |
# --- 4. ํต์ฌ ๋ก์ง ---
|
| 86 |
def format_history_for_llm(history):
|
| 87 |
llm_history = []
|
| 88 |
-
for
|
| 89 |
-
|
| 90 |
-
if
|
|
|
|
|
|
|
|
|
|
| 91 |
return llm_history
|
| 92 |
|
| 93 |
def chat(user_input, history, session_id, user_id):
|
| 94 |
if not user_input.strip(): return "", history
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
log_interaction(session_id, user_id, 'user', user_input)
|
| 97 |
yield "", history
|
|
|
|
| 98 |
llm_history = format_history_for_llm(history[:-1])
|
| 99 |
chat_session = model.start_chat(history=llm_history)
|
| 100 |
try:
|
| 101 |
response = chat_session.send_message(user_input, stream=True)
|
| 102 |
full_response = ""
|
|
|
|
| 103 |
for chunk in response:
|
| 104 |
full_response += chunk.text
|
| 105 |
-
history[-1][
|
| 106 |
yield "", history
|
| 107 |
log_interaction(session_id, user_id, 'model', full_response)
|
| 108 |
except Exception as e:
|
| 109 |
-
history
|
| 110 |
yield "", history
|
| 111 |
|
| 112 |
def change_topic(history, session_id, user_id):
|
| 113 |
-
if not history
|
| 114 |
llm_history = format_history_for_llm(history)
|
| 115 |
chat_session = model.start_chat(history=llm_history)
|
| 116 |
summary_prompt = (
|
|
@@ -122,10 +128,12 @@ def change_topic(history, session_id, user_id):
|
|
| 122 |
summary_text = response.text
|
| 123 |
log_summary(session_id, user_id, summary_text)
|
| 124 |
end_message = f"\n\n[๊ธฐ๋ก ์๋ฃ] ์ง๊ธ๊น์ง์ ์ด์ผ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ด ์ ๊ธฐ๋กํด ๋์์ต๋๋ค.\n\n๐ **์์ฝ:** {summary_text}\n\n์, ๊ทธ๋ผ ์ด์ ๋ค๋ฅธ ์ฃผ์ ๋ก ๋์ด๊ฐ ๋ณผ๊น์? ์ด๋ค ์ด์ผ๊ธฐ๋ฅผ ๋ ๋ค๋ ค์ฃผ์๊ฒ ์ด์?"
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
return history
|
| 127 |
except Exception as e:
|
| 128 |
-
history.append(
|
| 129 |
return history
|
| 130 |
|
| 131 |
def export_chat_txt(session_id):
|
|
@@ -172,7 +180,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 172 |
gr.Markdown("# ๐ ์ด์ผ๊ธฐ ๋น์ (ํ๋กํ ํ์
)")
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column(scale=3):
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
with gr.Row():
|
| 177 |
msg_input = gr.Textbox(scale=4, show_label=False, placeholder="์ฌ๊ธฐ์ ์ด์ผ๊ธฐ๋ฅผ ์
๋ ฅํ์ธ์...", container=False)
|
| 178 |
submit_btn = gr.Button("์ ์ก", scale=1, variant="primary")
|
|
@@ -202,14 +217,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 202 |
export_txt_btn.click(export_chat_txt, inputs=[session_id], outputs=[download_file])
|
| 203 |
export_md_btn.click(export_chat_md, inputs=[session_id], outputs=[download_file])
|
| 204 |
|
| 205 |
-
# --- 6. ์ฑ ์คํ
|
| 206 |
app_password = os.environ.get("APP_PASSWORD")
|
| 207 |
|
| 208 |
if __name__ == "__main__":
|
| 209 |
launch_kwargs = {
|
| 210 |
"server_name": "0.0.0.0",
|
| 211 |
"server_port": 7860,
|
| 212 |
-
"ssr_mode": False
|
| 213 |
}
|
| 214 |
if app_password:
|
| 215 |
launch_kwargs["auth"] = ("team", app_password)
|
|
|
|
| 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 ์ฐ๊ฒฐ ์ฑ๊ณต!")
|
|
|
|
| 59 |
# --- 3. LLM API ์ค์ ---
|
| 60 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 61 |
if not api_key:
|
|
|
|
| 62 |
try:
|
| 63 |
with open("api_key.txt", "r") as f: api_key = f.read().strip()
|
| 64 |
except: pass
|
|
|
|
| 84 |
# --- 4. ํต์ฌ ๋ก์ง ---
|
| 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':
|
| 92 |
+
llm_history.append({"role": "model", "parts": [{"text": msg['content']}]})
|
| 93 |
return llm_history
|
| 94 |
|
| 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
|
| 102 |
+
|
| 103 |
llm_history = format_history_for_llm(history[:-1])
|
| 104 |
chat_session = model.start_chat(history=llm_history)
|
| 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
|
| 112 |
yield "", history
|
| 113 |
log_interaction(session_id, user_id, 'model', full_response)
|
| 114 |
except Exception as e:
|
| 115 |
+
history.append({"role": "assistant", "content": f"โ ๏ธ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"})
|
| 116 |
yield "", history
|
| 117 |
|
| 118 |
def change_topic(history, session_id, user_id):
|
| 119 |
+
if not history: return history
|
| 120 |
llm_history = format_history_for_llm(history)
|
| 121 |
chat_session = model.start_chat(history=llm_history)
|
| 122 |
summary_prompt = (
|
|
|
|
| 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:
|
| 136 |
+
history.append({"role": "assistant", "content": f"โ ๏ธ ์์ฝ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"})
|
| 137 |
return history
|
| 138 |
|
| 139 |
def export_chat_txt(session_id):
|
|
|
|
| 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():
|
| 192 |
msg_input = gr.Textbox(scale=4, show_label=False, placeholder="์ฌ๊ธฐ์ ์ด์ผ๊ธฐ๋ฅผ ์
๋ ฅํ์ธ์...", container=False)
|
| 193 |
submit_btn = gr.Button("์ ์ก", scale=1, variant="primary")
|
|
|
|
| 217 |
export_txt_btn.click(export_chat_txt, inputs=[session_id], outputs=[download_file])
|
| 218 |
export_md_btn.click(export_chat_md, inputs=[session_id], outputs=[download_file])
|
| 219 |
|
| 220 |
+
# --- 6. ์ฑ ์คํ ---
|
| 221 |
app_password = os.environ.get("APP_PASSWORD")
|
| 222 |
|
| 223 |
if __name__ == "__main__":
|
| 224 |
launch_kwargs = {
|
| 225 |
"server_name": "0.0.0.0",
|
| 226 |
"server_port": 7860,
|
| 227 |
+
"ssr_mode": False
|
| 228 |
}
|
| 229 |
if app_password:
|
| 230 |
launch_kwargs["auth"] = ("team", app_password)
|