Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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__":
|