Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 6 |
import gradio as gr
|
| 7 |
from google import generativeai as genai
|
| 8 |
|
|
|
|
| 9 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 10 |
|
| 11 |
if API_KEY:
|
|
@@ -14,51 +15,69 @@ if API_KEY:
|
|
| 14 |
else:
|
| 15 |
raise ValueError("API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. Hugging Face Spaces์ Repository secrets์ 'GOOGLE_API_KEY'๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
|
| 16 |
|
|
|
|
| 17 |
df = pd.read_csv('https://raw.githubusercontent.com/kairess/mental-health-chatbot/master/wellness_dataset_original.csv')
|
| 18 |
df = df.drop(columns=['Unnamed: 3'], errors='ignore')
|
| 19 |
df = df.dropna(subset=['์ ์ ', '์ฑ๋ด'])
|
| 20 |
|
|
|
|
| 21 |
model = SentenceTransformer('jhgan/ko-sbert-nli')
|
| 22 |
|
| 23 |
print("๋ฐ์ดํฐ์
์๋ฒ ๋ฉ์ ๋ฏธ๋ฆฌ ๊ณ์ฐ ์ค์
๋๋ค. ์ด ๊ณผ์ ์ ์๊ฐ์ด ์์๋ฉ๋๋ค...")
|
|
|
|
| 24 |
df['embedding'] = df['์ ์ '].apply(lambda x: model.encode(x))
|
| 25 |
print("์๋ฒ ๋ฉ ๊ณ์ฐ์ด ์๋ฃ๋์์ต๋๋ค! ์ด์ ์ฑ๋ด ์๋ต์ด ํจ์ฌ ๋นจ๋ผ์ง๋๋ค.")
|
| 26 |
|
|
|
|
| 27 |
def call_gemini_api(question):
|
|
|
|
| 28 |
try:
|
| 29 |
-
llm_model = genai.GenerativeModel('gemini-2.
|
| 30 |
response = llm_model.generate_content(question)
|
| 31 |
return response.text
|
| 32 |
except Exception as e:
|
| 33 |
print(f"API ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 34 |
-
return
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
def chatbot(user_question):
|
|
|
|
| 39 |
try:
|
|
|
|
| 40 |
user_embedding = model.encode(user_question)
|
|
|
|
|
|
|
| 41 |
similarities = df['embedding'].apply(lambda x: cosine_similarity([user_embedding], [x])[0][0])
|
|
|
|
|
|
|
| 42 |
best_match_index = similarities.idxmax()
|
| 43 |
best_score = similarities.loc[best_match_index]
|
| 44 |
best_match_row = df.loc[best_match_index]
|
| 45 |
|
|
|
|
| 46 |
if best_score >= COSINE_SIMILARITY_THRESHOLD:
|
|
|
|
| 47 |
answer = best_match_row['์ฑ๋ด']
|
| 48 |
print(f"์ ์ฌ๋ ๊ธฐ๋ฐ ๋ต๋ณ. ์ ์: {best_score}")
|
| 49 |
return answer
|
| 50 |
else:
|
|
|
|
| 51 |
print(f"์ ์ฌ๋ ์๊ณ๊ฐ({COSINE_SIMILARITY_THRESHOLD}) ๋ฏธ๋ง. Gemini ๋ชจ๋ธ์ ํธ์ถํฉ๋๋ค. ์ ์: {best_score}")
|
| 52 |
return call_gemini_api(user_question)
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"์ฑ๋ด ์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 55 |
return f"์ฃ์กํฉ๋๋ค. ์ฑ๋ด ์คํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 56 |
|
|
|
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=chatbot,
|
| 59 |
inputs=gr.Textbox(lines=2, placeholder="์ง๋ฌธ์ ์
๋ ฅํด ์ฃผ์ธ์...", label="์ง๋ฌธ", elem_id="user_question_input"),
|
| 60 |
outputs=gr.Textbox(lines=5, label="์ฑ๋ด ๋ต๋ณ"),
|
| 61 |
title="๋๋ ์๋ด ์ฑ๋ด",
|
| 62 |
-
description="5๋ถ ๋์ ๋ํํ์ฌ ์ฃผ์๊ณ ๋ค์์ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๊ผญ ์ค๋ฌธ์กฐ์ฌ์ ์ฐธ์ฌํด์ฃผ์ธ์! https://forms.gle/eWtyejQaQntKbbxG8"
|
|
|
|
| 63 |
|
| 64 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
from google import generativeai as genai
|
| 8 |
|
| 9 |
+
# --- 1. ํ๊ฒฝ ์ค์ ๋ฐ ๋ฐ์ดํฐ ๋ก๋ ---
|
| 10 |
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 11 |
|
| 12 |
if API_KEY:
|
|
|
|
| 15 |
else:
|
| 16 |
raise ValueError("API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. Hugging Face Spaces์ Repository secrets์ 'GOOGLE_API_KEY'๋ฅผ ์ค์ ํด์ฃผ์ธ์.")
|
| 17 |
|
| 18 |
+
# ๋ฐ์ดํฐ ๋ก๋ ๋ฐ ์ ์ฒ๋ฆฌ
|
| 19 |
df = pd.read_csv('https://raw.githubusercontent.com/kairess/mental-health-chatbot/master/wellness_dataset_original.csv')
|
| 20 |
df = df.drop(columns=['Unnamed: 3'], errors='ignore')
|
| 21 |
df = df.dropna(subset=['์ ์ ', '์ฑ๋ด'])
|
| 22 |
|
| 23 |
+
# --- 2. ๋ชจ๋ธ ๋ก๋ ๋ฐ ์๋ฒ ๋ฉ ๊ณ์ฐ ---
|
| 24 |
model = SentenceTransformer('jhgan/ko-sbert-nli')
|
| 25 |
|
| 26 |
print("๋ฐ์ดํฐ์
์๋ฒ ๋ฉ์ ๋ฏธ๋ฆฌ ๊ณ์ฐ ์ค์
๋๋ค. ์ด ๊ณผ์ ์ ์๊ฐ์ด ์์๋ฉ๋๋ค...")
|
| 27 |
+
# '์ ์ ' ์ปฌ๋ผ์ ํ
์คํธ๋ฅผ ์๋ฒ ๋ฉ ๋ฒกํฐ๋ก ๋ณํํ์ฌ ์ ์ฅ
|
| 28 |
df['embedding'] = df['์ ์ '].apply(lambda x: model.encode(x))
|
| 29 |
print("์๋ฒ ๋ฉ ๊ณ์ฐ์ด ์๋ฃ๋์์ต๋๋ค! ์ด์ ์ฑ๋ด ์๋ต์ด ํจ์ฌ ๋นจ๋ผ์ง๋๋ค.")
|
| 30 |
|
| 31 |
+
# --- 3. Gemini API ํธ์ถ ํจ์ ---
|
| 32 |
def call_gemini_api(question):
|
| 33 |
+
"""์ ์ฌ๋ ์๊ณ๊ฐ ๋ฏธ๋ง ์ Gemini ๋ชจ๋ธ์ ํธ์ถํ์ฌ ์๋ต์ ์์ฑํฉ๋๋ค."""
|
| 34 |
try:
|
| 35 |
+
llm_model = genai.GenerativeModel('gemini-2.0-flash')
|
| 36 |
response = llm_model.generate_content(question)
|
| 37 |
return response.text
|
| 38 |
except Exception as e:
|
| 39 |
print(f"API ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 40 |
+
return f"์ฃ์กํฉ๋๋ค. API ํธ์ถ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 41 |
|
| 42 |
+
# --- 4. ์ฑ๋ด ํต์ฌ ๋ก์ง ํจ์ ---
|
| 43 |
+
COSINE_SIMILARITY_THRESHOLD = 0.7
|
| 44 |
|
| 45 |
def chatbot(user_question):
|
| 46 |
+
"""์ฌ์ฉ์ ์ง๋ฌธ์ ๋ํด ์ ์ฌ๋ ๊ฒ์ ํ, ์์ผ๋ฉด Gemini ๋ชจ๋ธ์ ํธ์ถํฉ๋๋ค."""
|
| 47 |
try:
|
| 48 |
+
# ์ฌ์ฉ์ ์ง๋ฌธ ์๋ฒ ๋ฉ
|
| 49 |
user_embedding = model.encode(user_question)
|
| 50 |
+
|
| 51 |
+
# ๋ฐ์ดํฐ์
์ ์ฒด์ ์ฝ์ฌ์ธ ์ ์ฌ๋ ๊ณ์ฐ
|
| 52 |
similarities = df['embedding'].apply(lambda x: cosine_similarity([user_embedding], [x])[0][0])
|
| 53 |
+
|
| 54 |
+
# ๊ฐ์ฅ ์ ์ฌํ ๋ต๋ณ ์ฐพ๊ธฐ
|
| 55 |
best_match_index = similarities.idxmax()
|
| 56 |
best_score = similarities.loc[best_match_index]
|
| 57 |
best_match_row = df.loc[best_match_index]
|
| 58 |
|
| 59 |
+
# ์ ์ฌ๋ ์๊ณ๊ฐ ๋น๊ต
|
| 60 |
if best_score >= COSINE_SIMILARITY_THRESHOLD:
|
| 61 |
+
# ์ ์ฌ๋ ๊ธฐ๋ฐ ๋ต๋ณ (Retrieval-based)
|
| 62 |
answer = best_match_row['์ฑ๋ด']
|
| 63 |
print(f"์ ์ฌ๋ ๊ธฐ๋ฐ ๋ต๋ณ. ์ ์: {best_score}")
|
| 64 |
return answer
|
| 65 |
else:
|
| 66 |
+
# Gemini ๋ชจ๋ธ ํธ์ถ (Generative-based)
|
| 67 |
print(f"์ ์ฌ๋ ์๊ณ๊ฐ({COSINE_SIMILARITY_THRESHOLD}) ๋ฏธ๋ง. Gemini ๋ชจ๋ธ์ ํธ์ถํฉ๋๋ค. ์ ์: {best_score}")
|
| 68 |
return call_gemini_api(user_question)
|
| 69 |
+
|
| 70 |
except Exception as e:
|
| 71 |
print(f"์ฑ๋ด ์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 72 |
return f"์ฃ์กํฉ๋๋ค. ์ฑ๋ด ์คํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 73 |
|
| 74 |
+
# --- 5. Gradio ์ธํฐํ์ด์ค ์คํ ---
|
| 75 |
demo = gr.Interface(
|
| 76 |
fn=chatbot,
|
| 77 |
inputs=gr.Textbox(lines=2, placeholder="์ง๋ฌธ์ ์
๋ ฅํด ์ฃผ์ธ์...", label="์ง๋ฌธ", elem_id="user_question_input"),
|
| 78 |
outputs=gr.Textbox(lines=5, label="์ฑ๋ด ๋ต๋ณ"),
|
| 79 |
title="๋๋ ์๋ด ์ฑ๋ด",
|
| 80 |
+
description="5๋ถ ๋์ ๋ํํ์ฌ ์ฃผ์๊ณ ๋ค์์ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๊ผญ ์ค๋ฌธ์กฐ์ฌ์ ์ฐธ์ฌํด์ฃผ์ธ์! https://forms.gle/eWtyejQaQntKbbxG8"
|
| 81 |
+
)
|
| 82 |
|
| 83 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|