Spaces:
Paused
Paused
Add application file
Browse files- app.py +14 -10
- utils/llm.py +66 -111
app.py
CHANGED
|
@@ -70,7 +70,13 @@ except Exception as e:
|
|
| 70 |
print(f"TTS μ»΄ν¬λνΈ μ΄κΈ°ν μ€ν¨: {e}")
|
| 71 |
tts = None
|
| 72 |
|
| 73 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
conversation_history = []
|
| 75 |
|
| 76 |
|
|
@@ -110,16 +116,12 @@ def process_audio(audio_path):
|
|
| 110 |
|
| 111 |
# 2λ¨κ³: LLMμΌλ‘ μλ΅ μμ±
|
| 112 |
try:
|
| 113 |
-
#
|
| 114 |
-
# μ¬κΈ°μλ κ°λ¨ν λ°λ‘ μ§μλ₯Ό μ λ¬ν©λλ€
|
| 115 |
-
context_data = [] # μ€μ λ‘λ κ²μ κ²°κ³Όλ₯Ό μ¬κΈ°μ λ£μ΅λλ€
|
| 116 |
-
|
| 117 |
-
# DeepSeek APIλ‘ μλ΅ μμ± (RAG λ°©μ)
|
| 118 |
user_query = conversation_history[-1]["content"] # λ§μ§λ§ μ¬μ©μ λ©μμ§
|
| 119 |
-
llm_response = llm.
|
| 120 |
except Exception as e:
|
| 121 |
print(f"LLM μ€λ₯: {e}")
|
| 122 |
-
llm_response = "
|
| 123 |
|
| 124 |
# AI μλ΅μ λν κΈ°λ‘μ μΆκ°
|
| 125 |
conversation_history.append({"role": "assistant", "content": llm_response})
|
|
@@ -186,10 +188,12 @@ with gr.Blocks(title="μμ± μ±λ΄") as app:
|
|
| 186 |
|
| 187 |
gr.Markdown("## μ¬μ© λ°©λ²")
|
| 188 |
gr.Markdown("""
|
| 189 |
-
1. λ§μ΄ν¬ λ²νΌμ ν΄λ¦νκ³ λ©μμ§λ₯Ό λ§νμΈμ
|
| 190 |
-
2. AIκ° μμ±μ μ²λ¦¬νκ³ μλ΅μ μμ±ν λκΉμ§ κΈ°λ€λ¦¬μΈμ
|
| 191 |
3. ν
μ€νΈ μλ΅μ μ½κ³ μμ± μλ΅μ λ€μΌμΈμ
|
| 192 |
4. "λν μ΄κΈ°ν" λ²νΌμ ν΄λ¦νμ¬ μ λνλ₯Ό μμνμΈμ
|
|
|
|
|
|
|
| 193 |
""")
|
| 194 |
|
| 195 |
gr.Markdown("## μμ€ν
μ 보")
|
|
|
|
| 70 |
print(f"TTS μ»΄ν¬λνΈ μ΄κΈ°ν μ€ν¨: {e}")
|
| 71 |
tts = None
|
| 72 |
|
| 73 |
+
# μ΄κΈ° λνλ¬Έ μ€μ
|
| 74 |
+
initialMessages = [
|
| 75 |
+
{"role": "system",
|
| 76 |
+
"content": "You are a helpful assistant. Always respond in English, regardless of the language used in the question. Keep your answers clear, concise, and helpful."}
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
# λν κΈ°λ‘μ μ μ₯ν μ μ λ³μ
|
| 80 |
conversation_history = []
|
| 81 |
|
| 82 |
|
|
|
|
| 116 |
|
| 117 |
# 2λ¨κ³: LLMμΌλ‘ μλ΅ μμ±
|
| 118 |
try:
|
| 119 |
+
# κ°λ¨ν μ§μ-μλ΅ λ°©μμΌλ‘ μμ²
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
user_query = conversation_history[-1]["content"] # λ§μ§λ§ μ¬μ©μ λ©μμ§
|
| 121 |
+
llm_response = llm.generate_response(user_query)
|
| 122 |
except Exception as e:
|
| 123 |
print(f"LLM μ€λ₯: {e}")
|
| 124 |
+
llm_response = "Sorry, I couldn't generate a response. Please try again."
|
| 125 |
|
| 126 |
# AI μλ΅μ λν κΈ°λ‘μ μΆκ°
|
| 127 |
conversation_history.append({"role": "assistant", "content": llm_response})
|
|
|
|
| 188 |
|
| 189 |
gr.Markdown("## μ¬μ© λ°©λ²")
|
| 190 |
gr.Markdown("""
|
| 191 |
+
1. λ§μ΄ν¬ λ²νΌμ ν΄λ¦νκ³ μμ΄λ‘ λ©μμ§λ₯Ό λ§νμΈμ
|
| 192 |
+
2. AIκ° μμ±μ μ²λ¦¬νκ³ μμ΄λ‘ μλ΅μ μμ±ν λκΉμ§ κΈ°λ€λ¦¬μΈμ
|
| 193 |
3. ν
μ€νΈ μλ΅μ μ½κ³ μμ± μλ΅μ λ€μΌμΈμ
|
| 194 |
4. "λν μ΄κΈ°ν" λ²νΌμ ν΄λ¦νμ¬ μ λνλ₯Ό μμνμΈμ
|
| 195 |
+
|
| 196 |
+
β οΈ μ°Έκ³ : μ΄ μ±λ΄μ μμ΄ μμ± μΈμκ³Ό μμ΄ μμ± ν©μ±μ μ¬μ©ν©λλ€.
|
| 197 |
""")
|
| 198 |
|
| 199 |
gr.Markdown("## μμ€ν
μ 보")
|
utils/llm.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import requests
|
| 2 |
-
import json
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
|
@@ -8,11 +7,13 @@ load_dotenv()
|
|
| 8 |
|
| 9 |
|
| 10 |
class DeepSeekLLM:
|
|
|
|
|
|
|
| 11 |
def __init__(self):
|
| 12 |
"""
|
| 13 |
-
DeepSeek API
|
| 14 |
|
| 15 |
-
νκ²½ λ³μμμ μ€μ μ κ°μ Έ
|
| 16 |
- DEEPSEEK_API_KEY: API ν€
|
| 17 |
- DEEPSEEK_ENDPOINT: API μλν¬μΈνΈ URL
|
| 18 |
- DEEPSEEK_MODEL: μ¬μ©ν λͺ¨λΈ μ΄λ¦
|
|
@@ -22,147 +23,101 @@ class DeepSeekLLM:
|
|
| 22 |
self.model = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")
|
| 23 |
|
| 24 |
if not self.api_key:
|
| 25 |
-
raise ValueError("DEEPSEEK_API_KEYκ°
|
| 26 |
|
| 27 |
-
print(f"DeepSeek
|
| 28 |
|
| 29 |
-
def generate_response(self,
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
|
| 33 |
Args:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
Returns:
|
| 39 |
-
str: μμ±λ μλ΅
|
| 40 |
"""
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
|
| 47 |
Args:
|
| 48 |
-
messages:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
Returns:
|
| 53 |
-
str: μμ±λ μλ΅
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
"""
|
| 55 |
headers = {
|
| 56 |
"Content-Type": "application/json",
|
| 57 |
"Authorization": f"Bearer {self.api_key}"
|
| 58 |
}
|
| 59 |
|
| 60 |
-
|
| 61 |
"model": self.model,
|
| 62 |
"messages": messages,
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
}
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
print(f"
|
| 69 |
-
response = requests.post(
|
| 70 |
-
self.endpoint,
|
| 71 |
-
headers=headers,
|
| 72 |
-
json=payload
|
| 73 |
-
)
|
| 74 |
|
|
|
|
| 75 |
if response.status_code == 200:
|
| 76 |
result = response.json()
|
| 77 |
-
print("[DeepSeek] μλ΅ μ±κ³΅")
|
| 78 |
-
|
| 79 |
-
# Node.js μ½λμ μ μ¬ν μλ΅ νμ±
|
| 80 |
if (result.get('choices') and
|
| 81 |
len(result['choices']) > 0 and
|
| 82 |
result['choices'][0].get('message') and
|
| 83 |
result['choices'][0]['message'].get('content')):
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
else:
|
| 86 |
-
print(f"
|
| 87 |
-
return "
|
| 88 |
else:
|
| 89 |
-
print(f"
|
| 90 |
-
print(f"
|
| 91 |
-
return f"
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
-
print(f"
|
| 95 |
-
return f"
|
| 96 |
-
|
| 97 |
-
def chat_with_rag(self, user_query, context_data=None):
|
| 98 |
-
"""
|
| 99 |
-
RAG(Retrieval-Augmented Generation) λ°©μμΌλ‘ ν둬ννΈλ₯Ό κ°ννμ¬ μ±ν
μλ΅ μμ±
|
| 100 |
-
|
| 101 |
-
Args:
|
| 102 |
-
user_query: μ¬μ©μ μ§λ¬Έ
|
| 103 |
-
context_data: κ²μλ 컨ν
μ€νΈ λ°μ΄ν° (Noneμ΄λ©΄ RAG μμ΄ μ§ν)
|
| 104 |
-
|
| 105 |
-
Returns:
|
| 106 |
-
str: μμ±λ μλ΅ ν
μ€νΈ
|
| 107 |
-
"""
|
| 108 |
-
# κΈ°λ³Έ μμ€ν
λ©μμ§
|
| 109 |
-
system_message = {"role": "system",
|
| 110 |
-
"content": "λ μλ£μ λ¬Έκ° μν μ λ§‘μ. κ·Έλ¦¬κ³ μ§λ¬Έμ λλΆλΆ 건κ°μ κ΄λ ¨λ μ§λ¬Έμ΄ μ¬κΊΌμΌ. μ΅λν μ λ¬Έμ μΈ λ¨μ΄λ λ°°μ νκ³ μ΄ν΄νκΈ° μ½κ³ κ°λ΅νκ² λ΅λ³μ ν΄μ€."}
|
| 111 |
-
|
| 112 |
-
if not context_data or len(context_data) == 0:
|
| 113 |
-
# 컨ν
μ€νΈ μμ΄ κΈ°λ³Έ λ©μμ§λ§ μ¬μ©
|
| 114 |
-
messages = [
|
| 115 |
-
system_message,
|
| 116 |
-
{"role": "user", "content": user_query}
|
| 117 |
-
]
|
| 118 |
-
else:
|
| 119 |
-
# RAG λ°©μμΌλ‘ ν둬ννΈ κ°ν
|
| 120 |
-
enhanced_prompt = self._enhance_prompt_with_context(user_query, context_data)
|
| 121 |
-
messages = [
|
| 122 |
-
system_message,
|
| 123 |
-
{"role": "user", "content": enhanced_prompt}
|
| 124 |
-
]
|
| 125 |
-
|
| 126 |
-
# DeepSeek API νΈμΆ
|
| 127 |
-
return self.chat_completion(messages)
|
| 128 |
-
|
| 129 |
-
def _enhance_prompt_with_context(self, query, context_data):
|
| 130 |
-
"""
|
| 131 |
-
κ²μ κ²°κ³Όλ₯Ό λ°νμΌλ‘ ν둬ννΈ κ°ν (Node.js μ½λ μ°Έμ‘°)
|
| 132 |
-
|
| 133 |
-
Args:
|
| 134 |
-
query: μ¬μ©μ μ§λ¬Έ
|
| 135 |
-
context_data: κ²μλ 컨ν
μ€νΈ λ°μ΄ν°
|
| 136 |
-
|
| 137 |
-
Returns:
|
| 138 |
-
str: κ°νλ ν둬ννΈ
|
| 139 |
-
"""
|
| 140 |
-
# κΈ°λ³Έ μμ€ν
ν둬ννΈμ κ²μ κ²°κ³Ό ν΅ν©
|
| 141 |
-
enhanced_prompt = "λΉμ μ μ λ¬Έ μλ£ μλ΄κ°μ
λλ€. λ€μ μ 보λ₯Ό λ°νμΌλ‘ μ¬μ©μ μ§λ¬Έμ λ΅λ³ν΄μ£ΌμΈμ:\n\n"
|
| 142 |
-
|
| 143 |
-
# μ 보μ μΆμ²μ μ λ’°μ± λͺ
μ
|
| 144 |
-
enhanced_prompt += "β» λ€μ μ 보λ μλ£ λ°μ΄ν°λ² μ΄μ€μμ κ²μλ κ²°κ³Όμ
λλ€.\n\n"
|
| 145 |
-
|
| 146 |
-
# κ²μλ 컨ν
μ€νΈ μ 보 ν¬λ§·ν
|
| 147 |
-
for i, item in enumerate(context_data):
|
| 148 |
-
enhanced_prompt += f"γμ°Έκ³ μλ£ {i + 1}γ\n"
|
| 149 |
-
|
| 150 |
-
# νλͺ©λ³λ‘ μ 리
|
| 151 |
-
for key, value in item.items():
|
| 152 |
-
if value and str(value).strip():
|
| 153 |
-
enhanced_prompt += f"- {key}: {value}\n"
|
| 154 |
-
|
| 155 |
-
enhanced_prompt += "\n"
|
| 156 |
-
|
| 157 |
-
# λ΅λ³ μμ± κ°μ΄λλΌμΈ μ 곡
|
| 158 |
-
enhanced_prompt += "γλ΅λ³ κ°μ΄λλΌμΈγ\n"
|
| 159 |
-
enhanced_prompt += "1. μ μ°Έκ³ μλ£μ μ 보λ₯Ό μ°μ μ μΌλ‘ νμ©νμΈμ.\n"
|
| 160 |
-
enhanced_prompt += "2. μ°Έκ³ μλ£μ λͺ
νν μ λ³΄κ° μλ κ²½μ°, μνμ μΌλ‘ μ νν μΌλ° μ§μμ νμ©νμΈμ.\n"
|
| 161 |
-
enhanced_prompt += "3. λΆνμ€ν μ 보λ μ 곡νμ§ λ§κ³ , νμμ μ λ¬Έμ μλ΄μ κΆμ νμΈμ.\n"
|
| 162 |
-
enhanced_prompt += "4. μ¬μ©μκ° μ½κ² μ΄ν΄ν μ μλ μ©μ΄λ₯Ό μ¬μ©νμΈμ.\n\n"
|
| 163 |
-
|
| 164 |
-
# μ¬μ©μ μ§λ¬Έ ν¬ν¨
|
| 165 |
-
enhanced_prompt += f"γμ¬μ©μ μ§λ¬Έγ {query}\n\n"
|
| 166 |
-
enhanced_prompt += "γλ΅λ³γ\n"
|
| 167 |
-
|
| 168 |
-
return enhanced_prompt
|
|
|
|
| 1 |
import requests
|
|
|
|
| 2 |
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
class DeepSeekLLM:
|
| 10 |
+
"""DeepSeek APIλ₯Ό μ¬μ©ν μΈμ΄ λͺ¨λΈ ν΄λμ€"""
|
| 11 |
+
|
| 12 |
def __init__(self):
|
| 13 |
"""
|
| 14 |
+
DeepSeek API μ΄κΈ°ν
|
| 15 |
|
| 16 |
+
νκ²½ λ³μμμ μ€μ μ κ°μ Έμ΄:
|
| 17 |
- DEEPSEEK_API_KEY: API ν€
|
| 18 |
- DEEPSEEK_ENDPOINT: API μλν¬μΈνΈ URL
|
| 19 |
- DEEPSEEK_MODEL: μ¬μ©ν λͺ¨λΈ μ΄λ¦
|
|
|
|
| 23 |
self.model = os.getenv("DEEPSEEK_MODEL", "deepseek-chat")
|
| 24 |
|
| 25 |
if not self.api_key:
|
| 26 |
+
raise ValueError("DEEPSEEK_API_KEY νκ²½ λ³μκ° μ€μ λμ΄ μμ§ μμ΅λλ€.")
|
| 27 |
|
| 28 |
+
print(f"DeepSeek LLM μ΄κΈ°ν: λͺ¨λΈ={self.model}, μλν¬μΈνΈ={'μ€μ λ¨' if self.endpoint else 'κΈ°λ³Έκ°'}")
|
| 29 |
|
| 30 |
+
def generate_response(self, query, temperature=0.7, max_tokens=500):
|
| 31 |
"""
|
| 32 |
+
λ¨μΌ μ§μμ λν μλ΅ μμ±
|
| 33 |
|
| 34 |
Args:
|
| 35 |
+
query: μ¬μ©μ μ§λ¬Έ
|
| 36 |
+
temperature: μλ΅μ λ€μμ± (0.0-1.0)
|
| 37 |
+
max_tokens: μ΅λ ν ν° μ
|
| 38 |
|
| 39 |
Returns:
|
| 40 |
+
str: μμ±λ μλ΅
|
| 41 |
"""
|
| 42 |
+
# μμ€ν
λ©μμ§: νμ μμ΄λ‘ μλ΅
|
| 43 |
+
system_message = "You are a helpful assistant. Always respond in English, regardless of the language used in the question. Keep your answers clear, concise, and helpful."
|
| 44 |
+
|
| 45 |
+
# λ©μμ§ κ΅¬μ±
|
| 46 |
+
messages = [
|
| 47 |
+
{"role": "system", "content": system_message},
|
| 48 |
+
{"role": "user", "content": query}
|
| 49 |
+
]
|
| 50 |
|
| 51 |
+
# API μμ²
|
| 52 |
+
return self._call_api(messages, temperature, max_tokens)
|
| 53 |
+
|
| 54 |
+
def chat_completion(self, messages, temperature=0.7, max_tokens=500):
|
| 55 |
"""
|
| 56 |
+
λν μ΄λ ₯μ ν¬ν¨ν μλ΅ μμ±
|
| 57 |
|
| 58 |
Args:
|
| 59 |
+
messages: λ©μμ§ λͺ©λ‘ [{"role": "user", "content": "..."}, ...]
|
| 60 |
+
temperature: μλ΅μ λ€μμ± (0.0-1.0)
|
| 61 |
+
max_tokens: μ΅λ ν ν° μ
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
+
str: μμ±λ μλ΅
|
| 65 |
+
"""
|
| 66 |
+
# 첫 λ©μμ§κ° μμ€ν
λ©μμ§κ° μλλ©΄ μΆκ°
|
| 67 |
+
if not messages or messages[0]["role"] != "system":
|
| 68 |
+
system_message = {"role": "system",
|
| 69 |
+
"content": "You are a helpful assistant. Always respond in English, regardless of the language used in the question."}
|
| 70 |
+
messages = [system_message] + messages
|
| 71 |
+
|
| 72 |
+
# API μμ²
|
| 73 |
+
return self._call_api(messages, temperature, max_tokens)
|
| 74 |
+
|
| 75 |
+
def _call_api(self, messages, temperature, max_tokens):
|
| 76 |
+
"""
|
| 77 |
+
DeepSeek API νΈμΆ
|
| 78 |
+
|
| 79 |
+
Args:
|
| 80 |
+
messages: λ©μμ§ λͺ©λ‘
|
| 81 |
+
temperature: μλ΅μ λ€μμ±
|
| 82 |
+
max_tokens: μ΅λ ν ν° μ
|
| 83 |
+
|
| 84 |
+
Returns:
|
| 85 |
+
str: μμ±λ μλ΅
|
| 86 |
"""
|
| 87 |
headers = {
|
| 88 |
"Content-Type": "application/json",
|
| 89 |
"Authorization": f"Bearer {self.api_key}"
|
| 90 |
}
|
| 91 |
|
| 92 |
+
data = {
|
| 93 |
"model": self.model,
|
| 94 |
"messages": messages,
|
| 95 |
+
"temperature": temperature,
|
| 96 |
+
"max_tokens": max_tokens
|
| 97 |
}
|
| 98 |
|
| 99 |
try:
|
| 100 |
+
print(f"DeepSeek API μμ²: λ©μμ§ μ={len(messages)}, μ¨λ={temperature}")
|
| 101 |
+
response = requests.post(self.endpoint, headers=headers, json=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# μλ΅ μ²λ¦¬
|
| 104 |
if response.status_code == 200:
|
| 105 |
result = response.json()
|
|
|
|
|
|
|
|
|
|
| 106 |
if (result.get('choices') and
|
| 107 |
len(result['choices']) > 0 and
|
| 108 |
result['choices'][0].get('message') and
|
| 109 |
result['choices'][0]['message'].get('content')):
|
| 110 |
+
content = result['choices'][0]['message']['content'].strip()
|
| 111 |
+
print(f"DeepSeek API μλ΅ μ±κ³΅: {len(content)} μ")
|
| 112 |
+
return content
|
| 113 |
else:
|
| 114 |
+
print(f"DeepSeek API μλ΅ νμ μ€λ₯: {result}")
|
| 115 |
+
return "I couldn't generate a response. Please try again."
|
| 116 |
else:
|
| 117 |
+
print(f"DeepSeek API μ€λ₯: μν μ½λ {response.status_code}")
|
| 118 |
+
print(f"μ€λ₯ μμΈ: {response.text}")
|
| 119 |
+
return f"An error occurred (Status code: {response.status_code})"
|
| 120 |
|
| 121 |
except Exception as e:
|
| 122 |
+
print(f"DeepSeek API μμΈ λ°μ: {str(e)}")
|
| 123 |
+
return f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|