Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,16 +10,14 @@ API_KEY = os.getenv("TOGETHER_API_KEY")
|
|
10 |
if not API_KEY:
|
11 |
raise ValueError("API key is missing! Make sure TOGETHER_API_KEY is set in the Secrets.")
|
12 |
|
13 |
-
# Initialize the Together client
|
14 |
@st.cache_resource
|
15 |
def get_client():
|
16 |
return Together(api_key=API_KEY)
|
17 |
|
18 |
def process_file(file) -> str:
|
19 |
-
"""Process uploaded file and return its content"""
|
20 |
if file is None:
|
21 |
return ""
|
22 |
-
|
23 |
try:
|
24 |
if file.type == "application/pdf":
|
25 |
text = ""
|
@@ -47,15 +45,28 @@ def generate_response(
|
|
47 |
client = get_client()
|
48 |
|
49 |
try:
|
50 |
-
# λ©μμ§
|
51 |
-
messages = [
|
52 |
|
53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
for user_msg, assistant_msg in history:
|
55 |
-
messages.append({
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
# νμ¬ λ©μμ§μ νμΌ λ΄μ©
|
59 |
current_content = message
|
60 |
if files:
|
61 |
file_contents = []
|
@@ -66,28 +77,40 @@ def generate_response(
|
|
66 |
if file_contents:
|
67 |
current_content = current_content + "\n\n" + "\n\n".join(file_contents)
|
68 |
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
# API νΈμΆ
|
72 |
try:
|
73 |
-
stream = client.chat.completions.create(
|
74 |
-
model="deepseek-ai/DeepSeek-R1",
|
75 |
-
messages=messages,
|
76 |
-
max_tokens=max_tokens,
|
77 |
-
temperature=temperature,
|
78 |
-
top_p=top_p,
|
79 |
-
stream=True
|
80 |
-
)
|
81 |
|
82 |
for chunk in stream:
|
83 |
-
if chunk.choices and chunk.choices[0].delta.content:
|
84 |
yield chunk.choices[0].delta.content
|
85 |
|
86 |
except Exception as e:
|
87 |
if "rate limit" in str(e).lower():
|
88 |
-
yield "
|
89 |
else:
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
except Exception as e:
|
93 |
yield f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
@@ -105,12 +128,12 @@ def main():
|
|
105 |
st.header("μ€μ ")
|
106 |
system_message = st.text_area(
|
107 |
"μμ€ν
λ©μμ§",
|
108 |
-
value="λΉμ μ κΉμ΄ μκ² μκ°νλ AIμ
λλ€. λ¬Έμ λ₯Ό κΉμ΄ κ³ λ €νκ³ μ²΄κ³μ μΈ μΆλ‘ κ³Όμ μ ν΅ν΄ μ¬λ°λ₯Έ ν΄κ²°μ±
μ
|
109 |
height=100
|
110 |
)
|
111 |
-
max_tokens = st.slider("μ΅λ ν ν° μ", 1,
|
112 |
-
temperature = st.slider("μ¨λ", 0.
|
113 |
-
top_p = st.slider("Top-p", 0.
|
114 |
uploaded_file = st.file_uploader(
|
115 |
"νμΌ μ
λ‘λ (μ νμ¬ν)",
|
116 |
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
|
@@ -119,12 +142,12 @@ def main():
|
|
119 |
|
120 |
for message in st.session_state.messages:
|
121 |
with st.chat_message(message["role"]):
|
122 |
-
st.
|
123 |
|
124 |
if prompt := st.chat_input("무μμ μκ³ μΆμΌμ κ°μ?"):
|
125 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
126 |
with st.chat_message("user"):
|
127 |
-
st.
|
128 |
|
129 |
with st.chat_message("assistant"):
|
130 |
response_placeholder = st.empty()
|
|
|
10 |
if not API_KEY:
|
11 |
raise ValueError("API key is missing! Make sure TOGETHER_API_KEY is set in the Secrets.")
|
12 |
|
|
|
13 |
@st.cache_resource
|
14 |
def get_client():
|
15 |
return Together(api_key=API_KEY)
|
16 |
|
17 |
def process_file(file) -> str:
|
|
|
18 |
if file is None:
|
19 |
return ""
|
20 |
+
|
21 |
try:
|
22 |
if file.type == "application/pdf":
|
23 |
text = ""
|
|
|
45 |
client = get_client()
|
46 |
|
47 |
try:
|
48 |
+
# λ©μμ§ λ°°μ΄ μ΄κΈ°ν
|
49 |
+
messages = []
|
50 |
|
51 |
+
# μμ€ν
λ©μμ§κ° μλ κ²½μ°μλ§ μΆκ°
|
52 |
+
if system_message.strip():
|
53 |
+
messages.append({
|
54 |
+
"role": "system",
|
55 |
+
"content": system_message
|
56 |
+
})
|
57 |
+
|
58 |
+
# λν νμ€ν 리 μΆκ°
|
59 |
for user_msg, assistant_msg in history:
|
60 |
+
messages.append({
|
61 |
+
"role": "user",
|
62 |
+
"content": user_msg
|
63 |
+
})
|
64 |
+
messages.append({
|
65 |
+
"role": "assistant",
|
66 |
+
"content": assistant_msg
|
67 |
+
})
|
68 |
|
69 |
+
# νμ¬ λ©μμ§μ νμΌ λ΄μ© μ€λΉ
|
70 |
current_content = message
|
71 |
if files:
|
72 |
file_contents = []
|
|
|
77 |
if file_contents:
|
78 |
current_content = current_content + "\n\n" + "\n\n".join(file_contents)
|
79 |
|
80 |
+
# νμ¬ λ©μμ§ μΆκ°
|
81 |
+
messages.append({
|
82 |
+
"role": "user",
|
83 |
+
"content": current_content
|
84 |
+
})
|
85 |
+
|
86 |
+
# API μμ² μ€μ
|
87 |
+
request_params = {
|
88 |
+
"model": "deepseek-ai/DeepSeek-R1",
|
89 |
+
"messages": messages,
|
90 |
+
"max_tokens": max_tokens,
|
91 |
+
"temperature": temperature,
|
92 |
+
"top_p": top_p,
|
93 |
+
"stream": True
|
94 |
+
}
|
95 |
|
96 |
+
# API νΈμΆ
|
97 |
try:
|
98 |
+
stream = client.chat.completions.create(**request_params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
for chunk in stream:
|
101 |
+
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
|
102 |
yield chunk.choices[0].delta.content
|
103 |
|
104 |
except Exception as e:
|
105 |
if "rate limit" in str(e).lower():
|
106 |
+
yield "API νΈμΆ νλμ λλ¬νμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
107 |
else:
|
108 |
+
error_message = str(e)
|
109 |
+
# Together.aiμ μ€λ₯ μλ΅ λΆμ
|
110 |
+
if "Input validation error" in error_message:
|
111 |
+
yield "μ
λ ₯ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€. μμ€ν
κ΄λ¦¬μμκ² λ¬Έμν΄μ£ΌμΈμ."
|
112 |
+
else:
|
113 |
+
yield f"API νΈμΆ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {error_message}"
|
114 |
|
115 |
except Exception as e:
|
116 |
yield f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
|
|
128 |
st.header("μ€μ ")
|
129 |
system_message = st.text_area(
|
130 |
"μμ€ν
λ©μμ§",
|
131 |
+
value="λΉμ μ κΉμ΄ μκ² μκ°νλ AIμ
λλ€. λ¬Έμ λ₯Ό κΉμ΄ κ³ λ €νκ³ μ²΄κ³μ μΈ μΆλ‘ κ³Όμ μ ν΅ν΄ μ¬λ°λ₯Έ ν΄κ²°μ±
μ λμΆνμΈμ. λ°λμ νκΈλ‘ λ΅λ³νμΈμ.",
|
132 |
height=100
|
133 |
)
|
134 |
+
max_tokens = st.slider("μ΅λ ν ν° μ", 1, 4096, 2048) # ν ν° μ ν μ‘°μ
|
135 |
+
temperature = st.slider("μ¨λ", 0.0, 2.0, 0.7, 0.1) # μ¨λ λ²μ μ‘°μ
|
136 |
+
top_p = st.slider("Top-p", 0.0, 1.0, 0.7, 0.1) # top_p λ²μ μ‘°μ
|
137 |
uploaded_file = st.file_uploader(
|
138 |
"νμΌ μ
λ‘λ (μ νμ¬ν)",
|
139 |
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
|
|
|
142 |
|
143 |
for message in st.session_state.messages:
|
144 |
with st.chat_message(message["role"]):
|
145 |
+
st.markdown(message["content"])
|
146 |
|
147 |
if prompt := st.chat_input("무μμ μκ³ μΆμΌμ κ°μ?"):
|
148 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
149 |
with st.chat_message("user"):
|
150 |
+
st.markdown(prompt)
|
151 |
|
152 |
with st.chat_message("assistant"):
|
153 |
response_placeholder = st.empty()
|