Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from typing import Iterator
|
|
| 5 |
from PIL import Image
|
| 6 |
import base64
|
| 7 |
from PyPDF2 import PdfReader
|
|
|
|
| 8 |
|
| 9 |
API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 10 |
if not API_KEY:
|
|
@@ -33,9 +34,28 @@ def process_file(file) -> str:
|
|
| 33 |
st.error(f"νμΌ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 34 |
return ""
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def generate_response(
|
| 37 |
message: str,
|
| 38 |
-
history: list
|
| 39 |
system_message: str,
|
| 40 |
max_tokens: int,
|
| 41 |
temperature: float,
|
|
@@ -50,13 +70,11 @@ def generate_response(
|
|
| 50 |
|
| 51 |
# μμ€ν
λ©μμ§ μΆκ°
|
| 52 |
if system_message.strip():
|
| 53 |
-
messages.append(
|
| 54 |
-
"role": "system",
|
| 55 |
-
"content": system_message
|
| 56 |
-
})
|
| 57 |
|
| 58 |
-
# λν νμ€ν 리 μΆκ°
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
# νμ¬ λ©μμ§μ νμΌ λ΄μ© μ€λΉ
|
| 62 |
current_content = message
|
|
@@ -70,10 +88,10 @@ def generate_response(
|
|
| 70 |
current_content = current_content + "\n\n" + "\n\n".join(file_contents)
|
| 71 |
|
| 72 |
# νμ¬ λ©μμ§ μΆκ°
|
| 73 |
-
messages.append(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
# API μμ²
|
| 79 |
try:
|
|
@@ -94,11 +112,11 @@ def generate_response(
|
|
| 94 |
if "rate limit" in str(e).lower():
|
| 95 |
yield "API νΈμΆ νλμ λλ¬νμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 96 |
else:
|
| 97 |
-
st.error(f"API μ€λ₯ μμΈ: {str(e)}")
|
| 98 |
yield "μ£μ‘ν©λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
-
st.error(f"μ 체 μ€λ₯ μμΈ: {str(e)}")
|
| 102 |
yield "μ€λ₯κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 103 |
|
| 104 |
def main():
|
|
@@ -107,9 +125,6 @@ def main():
|
|
| 107 |
# μΈμ
μν μ΄κΈ°ν
|
| 108 |
if "messages" not in st.session_state:
|
| 109 |
st.session_state.messages = []
|
| 110 |
-
|
| 111 |
-
if "conversation_history" not in st.session_state: # μλ‘μ΄ λν νμ€ν 리 μ μ₯μ
|
| 112 |
-
st.session_state.conversation_history = []
|
| 113 |
|
| 114 |
st.title("DeepSeek μ±ν
")
|
| 115 |
st.markdown("DeepSeek AI λͺ¨λΈκ³Ό λννμΈμ. νμν κ²½μ° νμΌμ μ
λ‘λν μ μμ΅λλ€.")
|
|
@@ -138,8 +153,8 @@ def main():
|
|
| 138 |
# μ±ν
μ
λ ₯
|
| 139 |
if prompt := st.chat_input("무μμ μκ³ μΆμΌμ κ°μ?"):
|
| 140 |
# μ¬μ©μ λ©μμ§ μΆκ°
|
| 141 |
-
|
| 142 |
-
st.session_state.
|
| 143 |
|
| 144 |
with st.chat_message("user"):
|
| 145 |
st.markdown(prompt)
|
|
@@ -152,7 +167,7 @@ def main():
|
|
| 152 |
# generate_response νΈμΆ
|
| 153 |
for response_chunk in generate_response(
|
| 154 |
prompt,
|
| 155 |
-
st.session_state.
|
| 156 |
system_message,
|
| 157 |
max_tokens,
|
| 158 |
temperature,
|
|
@@ -165,8 +180,8 @@ def main():
|
|
| 165 |
response_placeholder.markdown(full_response)
|
| 166 |
|
| 167 |
# μλ΅ μ μ₯
|
| 168 |
-
|
| 169 |
-
st.session_state.
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
main()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import base64
|
| 7 |
from PyPDF2 import PdfReader
|
| 8 |
+
import json # λλ²κΉ
μ© μΆκ°
|
| 9 |
|
| 10 |
API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 11 |
if not API_KEY:
|
|
|
|
| 34 |
st.error(f"νμΌ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
| 35 |
return ""
|
| 36 |
|
| 37 |
+
def format_message(role: str, content: str) -> dict:
|
| 38 |
+
"""API λ©μμ§ νμμ λ§κ² λ©μμ§λ₯Ό ν¬λ§·ν
ν©λλ€."""
|
| 39 |
+
return {
|
| 40 |
+
"role": role,
|
| 41 |
+
"content": content
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
def get_formatted_history(messages: list) -> list:
|
| 45 |
+
"""λν νμ€ν 리λ₯Ό API νμμ λ§κ² λ³νν©λλ€."""
|
| 46 |
+
formatted_messages = []
|
| 47 |
+
for msg in messages:
|
| 48 |
+
if isinstance(msg, dict) and "role" in msg and "content" in msg:
|
| 49 |
+
# μν μ΄ μ¬λ°λ₯Έμ§ νμΈνκ³ μμ
|
| 50 |
+
role = msg["role"]
|
| 51 |
+
if role not in ["system", "user", "assistant"]:
|
| 52 |
+
role = "user" if role == "human" else "assistant"
|
| 53 |
+
formatted_messages.append(format_message(role, msg["content"]))
|
| 54 |
+
return formatted_messages
|
| 55 |
+
|
| 56 |
def generate_response(
|
| 57 |
message: str,
|
| 58 |
+
history: list,
|
| 59 |
system_message: str,
|
| 60 |
max_tokens: int,
|
| 61 |
temperature: float,
|
|
|
|
| 70 |
|
| 71 |
# μμ€ν
λ©μμ§ μΆκ°
|
| 72 |
if system_message.strip():
|
| 73 |
+
messages.append(format_message("system", system_message))
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# λν νμ€ν 리 μΆκ°
|
| 76 |
+
formatted_history = get_formatted_history(history)
|
| 77 |
+
messages.extend(formatted_history)
|
| 78 |
|
| 79 |
# νμ¬ λ©μμ§μ νμΌ λ΄μ© μ€λΉ
|
| 80 |
current_content = message
|
|
|
|
| 88 |
current_content = current_content + "\n\n" + "\n\n".join(file_contents)
|
| 89 |
|
| 90 |
# νμ¬ λ©μμ§ μΆκ°
|
| 91 |
+
messages.append(format_message("user", current_content))
|
| 92 |
+
|
| 93 |
+
# λλ²κΉ
: API μμ² λ΄μ© μΆλ ₯
|
| 94 |
+
st.write("API μμ² λ©μμ§:", json.dumps(messages, ensure_ascii=False, indent=2))
|
| 95 |
|
| 96 |
# API μμ²
|
| 97 |
try:
|
|
|
|
| 112 |
if "rate limit" in str(e).lower():
|
| 113 |
yield "API νΈμΆ νλμ λλ¬νμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 114 |
else:
|
| 115 |
+
st.error(f"API μ€λ₯ μμΈ: {str(e)}")
|
| 116 |
yield "μ£μ‘ν©λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 117 |
|
| 118 |
except Exception as e:
|
| 119 |
+
st.error(f"μ 체 μ€λ₯ μμΈ: {str(e)}")
|
| 120 |
yield "μ€λ₯κ° λ°μνμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
| 121 |
|
| 122 |
def main():
|
|
|
|
| 125 |
# μΈμ
μν μ΄κΈ°ν
|
| 126 |
if "messages" not in st.session_state:
|
| 127 |
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
st.title("DeepSeek μ±ν
")
|
| 130 |
st.markdown("DeepSeek AI λͺ¨λΈκ³Ό λννμΈμ. νμν κ²½μ° νμΌμ μ
λ‘λν μ μμ΅λλ€.")
|
|
|
|
| 153 |
# μ±ν
μ
λ ₯
|
| 154 |
if prompt := st.chat_input("무μμ μκ³ μΆμΌμ κ°μ?"):
|
| 155 |
# μ¬μ©μ λ©μμ§ μΆκ°
|
| 156 |
+
user_message = format_message("user", prompt)
|
| 157 |
+
st.session_state.messages.append(user_message)
|
| 158 |
|
| 159 |
with st.chat_message("user"):
|
| 160 |
st.markdown(prompt)
|
|
|
|
| 167 |
# generate_response νΈμΆ
|
| 168 |
for response_chunk in generate_response(
|
| 169 |
prompt,
|
| 170 |
+
st.session_state.messages,
|
| 171 |
system_message,
|
| 172 |
max_tokens,
|
| 173 |
temperature,
|
|
|
|
| 180 |
response_placeholder.markdown(full_response)
|
| 181 |
|
| 182 |
# μλ΅ μ μ₯
|
| 183 |
+
assistant_message = format_message("assistant", full_response)
|
| 184 |
+
st.session_state.messages.append(assistant_message)
|
| 185 |
|
| 186 |
if __name__ == "__main__":
|
| 187 |
main()
|