jonghhhh commited on
Commit
5e96ac1
Β·
verified Β·
1 Parent(s): 67d41dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -64
app.py CHANGED
@@ -2,55 +2,6 @@ import os
2
  import asyncio
3
  from typing import List, Dict
4
 
5
- # .env 파일 및 Space Secrets λ‘œλ“œ
6
- from dotenv import load_dotenv
7
- load_dotenv()
8
-
9
- # Google API Key 검증
10
- google_api_key = None
11
-
12
- # ν™˜κ²½λ³€μˆ˜μ—μ„œ κ°€μ Έμ˜€κΈ°
13
- google_api_key = os.getenv("GOOGLE_API_KEY")
14
- if not google_api_key:
15
- google_api_key = os.environ.get("GOOGLE_API_KEY")
16
-
17
- # .env νŒŒμΌμ—μ„œ 직접 읽기
18
- if not google_api_key:
19
- try:
20
- with open('.env', 'r', encoding='utf-8') as f:
21
- for line in f:
22
- line = line.strip()
23
- if line.startswith('GOOGLE_API_KEY='):
24
- google_api_key = line.split('=', 1)[1].strip().strip('"').strip("'")
25
- break
26
- except:
27
- pass
28
-
29
- # ν˜„μž¬ λ””λ ‰ν† λ¦¬μ˜ λͺ¨λ“  .env 파일 확인
30
- if not google_api_key:
31
- import glob
32
- for env_file in glob.glob("*.env"):
33
- try:
34
- with open(env_file, 'r', encoding='utf-8') as f:
35
- for line in f:
36
- line = line.strip()
37
- if line.startswith('GOOGLE_API_KEY='):
38
- google_api_key = line.split('=', 1)[1].strip().strip('"').strip("'")
39
- break
40
- if google_api_key:
41
- break
42
- except:
43
- pass
44
-
45
- # API ν‚€ μœ νš¨μ„± 검사
46
- if google_api_key:
47
- google_api_key = google_api_key.strip()
48
- if len(google_api_key) < 10 or not google_api_key.startswith('AI'):
49
- google_api_key = None
50
-
51
- if not google_api_key:
52
- raise EnvironmentError("GOOGLE_API_KEYλ₯Ό .env 파일 λ˜λŠ” ν™˜κ²½λ³€μˆ˜μ— μΆ”κ°€ν•΄μ£Όμ„Έμš”.")
53
-
54
  # ChromaDB 경둜 μ„€μ •
55
  db_dir = os.path.join(os.getcwd(), "chromadb_KH_media")
56
  os.environ["CHROMA_DB_DIR"] = db_dir
@@ -61,6 +12,13 @@ import gradio as gr
61
  from sentence_transformers import SentenceTransformer
62
  from google import genai
63
 
 
 
 
 
 
 
 
64
  # === Simple RAG μ‹œμŠ€ν…œ ===
65
  class SimpleRAGSystem:
66
  def __init__(self, db_path: str = None, collection: str = "KH_media_docs"):
@@ -94,18 +52,7 @@ except Exception:
94
  rag_system = None
95
 
96
  # === Google GenAI Client μ„€μ • ===
97
- try:
98
- os.environ["GOOGLE_API_KEY"] = google_api_key
99
- client = genai.Client(api_key=google_api_key)
100
-
101
- # κ°„λ‹¨ν•œ μ—°κ²° ν…ŒμŠ€νŠΈ
102
- test_response = client.models.generate_content(
103
- model="gemini-1.5-flash",
104
- contents="test",
105
- config={"max_output_tokens": 5}
106
- )
107
- except Exception as e:
108
- raise RuntimeError(f"Google GenAI μ΄ˆκΈ°ν™” μ‹€νŒ¨: {e}")
109
 
110
  # === μ‹œμŠ€ν…œ λ©”μ‹œμ§€ ===
111
  SYSTEM_MESSAGE = """당신은 κ²½ν¬λŒ€ν•™κ΅ λ―Έλ””μ–΄ν•™κ³Ό μ „λ¬Έ 상담 AIμž…λ‹ˆλ‹€.
@@ -128,7 +75,8 @@ SYSTEM_MESSAGE = """당신은 κ²½ν¬λŒ€ν•™κ΅ λ―Έλ””μ–΄ν•™κ³Ό μ „λ¬Έ 상담 AI
128
 
129
  # ν˜„μž¬ κ²½ν¬λŒ€ν•™κ΅ λ―Έλ””μ–΄ν•™κ³Ό κ΅μˆ˜μ§„:
130
  이인희, κΉ€νƒœμš©, λ°•μ’…λ―Ό, 홍지아, 이정ꡐ, μ΄κΈ°ν˜•, μ΄μ„ μ˜, 쑰수영, μ΄μ’…ν˜, 이두황, 이상원, μ΄ν›ˆ, μ΅œμˆ˜μ§„, μ΅œλ―Όμ•„, κΉ€κ΄€ν˜Έ
131
- """
 
132
 
133
  def respond(
134
  message,
@@ -263,8 +211,6 @@ with demo:
263
  **λ¬Έμ„œ 수**: {rag_system.collection.count() if rag_system and rag_system.available else "0"}개
264
 
265
  πŸ’‘ **μ‚¬μš© 팁**: ꡬ체적인 질문일수둝 더 μ •ν™•ν•œ 닡변을 받을 수 μžˆμŠ΅λ‹ˆλ‹€!
266
-
267
- πŸ”‘ **API μƒνƒœ**: {"βœ… Google API 연결됨" if google_api_key else "❌ API ν‚€ μ—†μŒ"}
268
  """)
269
 
270
  if __name__ == "__main__":
 
2
  import asyncio
3
  from typing import List, Dict
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # ChromaDB 경둜 μ„€μ •
6
  db_dir = os.path.join(os.getcwd(), "chromadb_KH_media")
7
  os.environ["CHROMA_DB_DIR"] = db_dir
 
12
  from sentence_transformers import SentenceTransformer
13
  from google import genai
14
 
15
+ # === ν˜„μž¬ κ΅μˆ˜μ§„ λͺ©λ‘ ===
16
+ PROFESSORS = [
17
+ "이인희", "κΉ€νƒœμš©", "λ°•μ’…λ―Ό", "홍지아", "이정ꡐ",
18
+ "μ΄κΈ°ν˜•", "μ΄μ„ μ˜", "쑰수영", "μ΄μ’…ν˜", "이두황",
19
+ "이상원", "μ΄ν›ˆ", "μ΅œμˆ˜μ§„", "μ΅œλ―Όμ•„", "κΉ€κ΄€ν˜Έ"
20
+ ]
21
+
22
  # === Simple RAG μ‹œμŠ€ν…œ ===
23
  class SimpleRAGSystem:
24
  def __init__(self, db_path: str = None, collection: str = "KH_media_docs"):
 
52
  rag_system = None
53
 
54
  # === Google GenAI Client μ„€μ • ===
55
+ client = genai.Client(api_key="AIzaSyBuBsC5k9yw2JwUfVFn1Zu1qM_ifwGx6cM")
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # === μ‹œμŠ€ν…œ λ©”μ‹œμ§€ ===
58
  SYSTEM_MESSAGE = """당신은 κ²½ν¬λŒ€ν•™κ΅ λ―Έλ””μ–΄ν•™κ³Ό μ „λ¬Έ 상담 AIμž…λ‹ˆλ‹€.
 
75
 
76
  # ν˜„μž¬ κ²½ν¬λŒ€ν•™κ΅ λ―Έλ””μ–΄ν•™κ³Ό κ΅μˆ˜μ§„:
77
  이인희, κΉ€νƒœμš©, λ°•μ’…λ―Ό, 홍지아, 이정ꡐ, μ΄κΈ°ν˜•, μ΄μ„ μ˜, 쑰수영, μ΄μ’…ν˜, 이두황, 이상원, μ΄ν›ˆ, μ΅œμˆ˜μ§„, μ΅œλ―Όμ•„, κΉ€κ΄€ν˜Έ
78
+
79
+ ν•œκ΅­μ–΄λ‘œ μžμ—°μŠ€λŸ½κ²Œ λ‹΅λ³€ν•΄μ£Όμ„Έμš”."""
80
 
81
  def respond(
82
  message,
 
211
  **λ¬Έμ„œ 수**: {rag_system.collection.count() if rag_system and rag_system.available else "0"}개
212
 
213
  πŸ’‘ **μ‚¬μš© 팁**: ꡬ체적인 질문일수둝 더 μ •ν™•ν•œ 닡변을 받을 수 μžˆμŠ΅λ‹ˆλ‹€!
 
 
214
  """)
215
 
216
  if __name__ == "__main__":