Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from google import generativeai as genai
|
| 8 |
+
|
| 9 |
+
# API ํค๋ฅผ Hugging Face Spaces์ Repository secrets์์ ๊ฐ์ ธ์ต๋๋ค.
|
| 10 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 11 |
+
|
| 12 |
+
if API_KEY:
|
| 13 |
+
genai.configure(api_key=API_KEY)
|
| 14 |
+
print("API ํค๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์ค์ ๋์์ต๋๋ค.")
|
| 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 |
+
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋
|
| 24 |
+
model = SentenceTransformer('jhgan/ko-sbert-nli')
|
| 25 |
+
|
| 26 |
+
# ๋ฐ์ดํฐ์
์๋ฒ ๋ฉ ๋ฏธ๋ฆฌ ๊ณ์ฐ
|
| 27 |
+
print("๋ฐ์ดํฐ์
์๋ฒ ๋ฉ์ ๋ฏธ๋ฆฌ ๊ณ์ฐ ์ค์
๋๋ค. ์ด ๊ณผ์ ์ ์๊ฐ์ด ์์๋ฉ๋๋ค...")
|
| 28 |
+
df['embedding'] = df['์ ์ '].apply(lambda x: model.encode(x))
|
| 29 |
+
print("์๋ฒ ๋ฉ ๊ณ์ฐ์ด ์๋ฃ๋์์ต๋๋ค! ์ด์ ์ฑ๋ด ์๋ต์ด ํจ์ฌ ๋นจ๋ผ์ง๋๋ค.")
|
| 30 |
+
|
| 31 |
+
# Gemini API ํธ์ถ ํจ์
|
| 32 |
+
def call_gemini_api(question):
|
| 33 |
+
try:
|
| 34 |
+
llm_model = genai.GenerativeModel('gemini-pro')
|
| 35 |
+
response = llm_model.generate_content(question)
|
| 36 |
+
return response.text
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"API ํธ์ถ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 39 |
+
return f"์ฃ์กํฉ๋๋ค. API ํธ์ถ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 40 |
+
|
| 41 |
+
# ์ ์ฌ๋ ์๊ณ๊ฐ
|
| 42 |
+
COSINE_SIMILARITY_THRESHOLD = 0.8
|
| 43 |
+
|
| 44 |
+
# ์ฑ๋ด ํต์ฌ ๋ก์ง
|
| 45 |
+
def chatbot(user_question):
|
| 46 |
+
try:
|
| 47 |
+
user_embedding = model.encode(user_question)
|
| 48 |
+
similarities = df['embedding'].apply(lambda x: cosine_similarity([user_embedding], [x])[0][0])
|
| 49 |
+
best_match_index = similarities.idxmax()
|
| 50 |
+
best_score = similarities.loc[best_match_index]
|
| 51 |
+
best_match_row = df.loc[best_match_index]
|
| 52 |
+
|
| 53 |
+
if best_score >= COSINE_SIMILARITY_THRESHOLD:
|
| 54 |
+
answer = best_match_row['์ฑ๋ด']
|
| 55 |
+
print(f"์ ์ฌ๋ ๊ธฐ๋ฐ ๋ต๋ณ. ์ ์: {best_score}")
|
| 56 |
+
return answer
|
| 57 |
+
else:
|
| 58 |
+
print(f"์ ์ฌ๋ ์๊ณ๊ฐ({COSINE_SIMILARITY_THRESHOLD}) ๋ฏธ๋ง. Gemini ๋ชจ๋ธ์ ํธ์ถํฉ๋๋ค. ์ ์: {best_score}")
|
| 59 |
+
return call_gemini_api(user_question)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"์ฑ๋ด ์คํ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 62 |
+
return f"์ฃ์กํฉ๋๋ค. ์ฑ๋ด ์คํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}"
|
| 63 |
+
|
| 64 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
| 65 |
+
demo = gr.Interface(
|
| 66 |
+
fn=chatbot,
|
| 67 |
+
inputs=gr.Textbox(lines=2, placeholder="์ง๋ฌธ์ ์
๋ ฅํด ์ฃผ์ธ์...", label="์ง๋ฌธ", elem_id="user_question_input"),
|
| 68 |
+
outputs=gr.Textbox(lines=5, label="์ฑ๋ด ๋ต๋ณ"),
|
| 69 |
+
title="๋๋ ์๋ด ์ฑ๋ด",
|
| 70 |
+
description="5๋ถ ๋์ ๋ํํ์ฌ ์ฃผ์๊ณ ๋ค์์ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๊ผญ ์ค๋ฌธ์กฐ์ฌ์ ์ฐธ์ฌํด์ฃผ์ธ์! https://forms.gle/eWtyejQaQntKbbxG8"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Hugging Face Spaces ํ๊ฒฝ์ ๋ง์ถฐ server_name๊ณผ server_port๋ฅผ ๋ช
์ํฉ๋๋ค.
|
| 74 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|