messy092 commited on
Commit
700cfab
ยท
verified ยท
1 Parent(s): 47108d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -9
app.py CHANGED
@@ -6,7 +6,6 @@ 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:
@@ -15,20 +14,16 @@ 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
- # ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ ๋กœ๋“œ
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')
@@ -38,10 +33,8 @@ def call_gemini_api(question):
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)
@@ -61,7 +54,6 @@ def chatbot(user_question):
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"),
@@ -70,5 +62,4 @@ demo = gr.Interface(
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)
 
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
  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-pro')
 
33
  print(f"API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
34
  return f"์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. API ํ˜ธ์ถœ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {e}"
35
 
 
36
  COSINE_SIMILARITY_THRESHOLD = 0.8
37
 
 
38
  def chatbot(user_question):
39
  try:
40
  user_embedding = model.encode(user_question)
 
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"),
 
62
  description="5๋ถ„ ๋™์•ˆ ๋Œ€ํ™”ํ•˜์—ฌ ์ฃผ์‹œ๊ณ  ๋‹ค์Œ์˜ ๋งํฌ๋ฅผ ํด๋ฆญํ•˜์—ฌ ๊ผญ ์„ค๋ฌธ์กฐ์‚ฌ์— ์ฐธ์—ฌํ•ด์ฃผ์„ธ์š”! https://forms.gle/eWtyejQaQntKbbxG8"
63
  )
64
 
 
65
  demo.launch(server_name="0.0.0.0", server_port=7860, share=False)