Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,19 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
import os
|
4 |
from typing import Iterator
|
5 |
from PIL import Image
|
6 |
-
import pytesseract
|
7 |
-
from PyPDF2 import PdfReader
|
8 |
import base64
|
9 |
-
from
|
10 |
|
11 |
API_KEY = os.getenv("TOGETHER_API_KEY")
|
12 |
if not API_KEY:
|
13 |
raise ValueError("API key is missing! Make sure TOGETHER_API_KEY is set in the Secrets.")
|
14 |
|
15 |
-
|
16 |
-
# Initialize the client with Together AI provider
|
17 |
@st.cache_resource
|
18 |
def get_client():
|
19 |
-
|
20 |
-
# provider="together",
|
21 |
-
# api_key=API_KEY
|
22 |
-
#)
|
23 |
-
return Together(api_key=API_KEY) # Use Together.ai's official client
|
24 |
|
25 |
def process_file(file) -> str:
|
26 |
"""Process uploaded file and return its content"""
|
@@ -28,25 +21,19 @@ def process_file(file) -> str:
|
|
28 |
return ""
|
29 |
|
30 |
try:
|
31 |
-
# Handle PDF files
|
32 |
if file.type == "application/pdf":
|
33 |
text = ""
|
34 |
pdf_reader = PdfReader(file)
|
35 |
for page in pdf_reader.pages:
|
36 |
-
|
37 |
-
if page_text:
|
38 |
-
text += page_text + "\n"
|
39 |
return text
|
40 |
-
|
41 |
-
# Handle image files
|
42 |
elif file.type.startswith("image/"):
|
43 |
return base64.b64encode(file.getvalue()).decode("utf-8")
|
44 |
-
|
45 |
-
# Handle text files
|
46 |
else:
|
47 |
return file.getvalue().decode('utf-8')
|
48 |
except Exception as e:
|
49 |
-
|
|
|
50 |
|
51 |
def generate_response(
|
52 |
message: str,
|
@@ -58,63 +45,31 @@ def generate_response(
|
|
58 |
files=None
|
59 |
) -> Iterator[str]:
|
60 |
client = get_client()
|
61 |
-
|
62 |
-
has_images = False
|
63 |
-
content_blocks = []
|
64 |
-
image_content = None # To store image data
|
65 |
-
image_mime_type = None # To store MIME type
|
66 |
-
|
67 |
-
if files:
|
68 |
-
for file in files:
|
69 |
-
content = process_file(file)
|
70 |
-
if file.type.startswith("image/"):
|
71 |
-
has_images = True
|
72 |
-
image_content = content # Already base64 encoded
|
73 |
-
image_mime_type = file.type # Store MIME type
|
74 |
-
else:
|
75 |
-
content_blocks.append({
|
76 |
-
"type": "text",
|
77 |
-
"text": f"File content:\n{content}"
|
78 |
-
})
|
79 |
-
|
80 |
-
# Build messages
|
81 |
-
messages = [{"role": "system", "content": system_message}]
|
82 |
-
|
83 |
-
# Add history
|
84 |
-
for user_msg, assistant_msg in history:
|
85 |
-
messages.append({"role": "user", "content": user_msg})
|
86 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
87 |
-
|
88 |
try:
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
current_message = {
|
113 |
-
"role": "user",
|
114 |
-
"content": [{"type": "text", "text": message}] + content_blocks
|
115 |
-
}
|
116 |
-
messages.append(current_message)
|
117 |
-
|
118 |
stream = client.chat.completions.create(
|
119 |
model="deepseek-ai/DeepSeek-R1",
|
120 |
messages=messages,
|
@@ -123,84 +78,61 @@ def generate_response(
|
|
123 |
top_p=top_p,
|
124 |
stream=True
|
125 |
)
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
132 |
except Exception as e:
|
133 |
-
yield f"
|
|
|
134 |
def main():
|
135 |
-
st.set_page_config(page_title="DeepSeek
|
136 |
|
137 |
-
# Initialize session state for chat history
|
138 |
if "messages" not in st.session_state:
|
139 |
st.session_state.messages = []
|
140 |
|
141 |
-
st.title("DeepSeek
|
142 |
-
st.markdown("
|
143 |
-
st.markdown("Feel free to upload images too, in this case Llama Vision will be used")
|
144 |
|
145 |
-
# Sidebar for parameters
|
146 |
with st.sidebar:
|
147 |
-
st.header("
|
148 |
system_message = st.text_area(
|
149 |
-
"
|
150 |
-
value="
|
151 |
height=100
|
152 |
)
|
153 |
-
max_tokens = st.slider(
|
154 |
-
|
155 |
-
|
156 |
-
max_value=8192,
|
157 |
-
value=8192,
|
158 |
-
step=1
|
159 |
-
)
|
160 |
-
temperature = st.slider(
|
161 |
-
"Temperature",
|
162 |
-
min_value=0.1,
|
163 |
-
max_value=4.0,
|
164 |
-
value=0.0,
|
165 |
-
step=0.1
|
166 |
-
)
|
167 |
-
top_p = st.slider(
|
168 |
-
"Top-p (nucleus sampling)",
|
169 |
-
min_value=0.1,
|
170 |
-
max_value=1.0,
|
171 |
-
value=0.95,
|
172 |
-
step=0.05
|
173 |
-
)
|
174 |
uploaded_file = st.file_uploader(
|
175 |
-
"
|
176 |
-
type=['txt', 'py', 'md', '
|
177 |
-
'php', 'c', 'cpp', 'h', 'hpp', 'cs', 'html', 'css', 'kt', 'svelte',
|
178 |
-
'pdf', 'png', 'jpg', 'jpeg'], # Added file types
|
179 |
accept_multiple_files=True
|
180 |
)
|
181 |
|
182 |
-
# Display chat messages
|
183 |
for message in st.session_state.messages:
|
184 |
with st.chat_message(message["role"]):
|
185 |
st.write(message["content"])
|
186 |
|
187 |
-
|
188 |
-
if prompt := st.chat_input("What would you like to know?"):
|
189 |
-
# Display user message
|
190 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
191 |
with st.chat_message("user"):
|
192 |
st.write(prompt)
|
193 |
|
194 |
-
# Generate and display assistant response
|
195 |
with st.chat_message("assistant"):
|
196 |
response_placeholder = st.empty()
|
197 |
full_response = ""
|
198 |
|
199 |
-
# Get message history for context
|
200 |
history = [(msg["content"], next_msg["content"])
|
201 |
for msg, next_msg in zip(st.session_state.messages[::2], st.session_state.messages[1::2])]
|
202 |
|
203 |
-
# Stream the response
|
204 |
for response_chunk in generate_response(
|
205 |
prompt,
|
206 |
history,
|
@@ -211,12 +143,10 @@ def main():
|
|
211 |
uploaded_file
|
212 |
):
|
213 |
full_response += response_chunk
|
214 |
-
print(full_response)
|
215 |
response_placeholder.markdown(full_response + "β")
|
216 |
|
217 |
response_placeholder.markdown(full_response)
|
218 |
|
219 |
-
# Add assistant response to chat history
|
220 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
221 |
|
222 |
if __name__ == "__main__":
|
|
|
1 |
import streamlit as st
|
2 |
+
from together import Together
|
3 |
import os
|
4 |
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:
|
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"""
|
|
|
21 |
return ""
|
22 |
|
23 |
try:
|
|
|
24 |
if file.type == "application/pdf":
|
25 |
text = ""
|
26 |
pdf_reader = PdfReader(file)
|
27 |
for page in pdf_reader.pages:
|
28 |
+
text += page.extract_text() + "\n"
|
|
|
|
|
29 |
return text
|
|
|
|
|
30 |
elif file.type.startswith("image/"):
|
31 |
return base64.b64encode(file.getvalue()).decode("utf-8")
|
|
|
|
|
32 |
else:
|
33 |
return file.getvalue().decode('utf-8')
|
34 |
except Exception as e:
|
35 |
+
st.error(f"νμΌ μ²λ¦¬ μ€ μ€λ₯ λ°μ: {str(e)}")
|
36 |
+
return ""
|
37 |
|
38 |
def generate_response(
|
39 |
message: str,
|
|
|
45 |
files=None
|
46 |
) -> Iterator[str]:
|
47 |
client = get_client()
|
48 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
try:
|
50 |
+
# λ©μμ§ νμ μμ
|
51 |
+
messages = [{"role": "system", "content": system_message}]
|
52 |
+
|
53 |
+
# νμ€ν 리 μΆκ°
|
54 |
+
for user_msg, assistant_msg in history:
|
55 |
+
messages.append({"role": "user", "content": user_msg})
|
56 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
57 |
+
|
58 |
+
# νμ¬ λ©μμ§μ νμΌ λ΄μ© μΆκ°
|
59 |
+
current_content = message
|
60 |
+
if files:
|
61 |
+
file_contents = []
|
62 |
+
for file in files:
|
63 |
+
content = process_file(file)
|
64 |
+
if content:
|
65 |
+
file_contents.append(f"νμΌ λ΄μ©:\n{content}")
|
66 |
+
if file_contents:
|
67 |
+
current_content = current_content + "\n\n" + "\n\n".join(file_contents)
|
68 |
+
|
69 |
+
messages.append({"role": "user", "content": current_content})
|
70 |
+
|
71 |
+
# API νΈμΆ μλ
|
72 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
stream = client.chat.completions.create(
|
74 |
model="deepseek-ai/DeepSeek-R1",
|
75 |
messages=messages,
|
|
|
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 "μ£μ‘ν©λλ€. API νΈμΆ νλμ λλ¬νμ΅λλ€. μ μ ν λ€μ μλν΄μ£ΌμΈμ."
|
89 |
+
else:
|
90 |
+
yield f"API νΈμΆ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
91 |
+
|
92 |
except Exception as e:
|
93 |
+
yield f"μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
94 |
+
|
95 |
def main():
|
96 |
+
st.set_page_config(page_title="DeepSeek μ±ν
", page_icon="π", layout="wide")
|
97 |
|
|
|
98 |
if "messages" not in st.session_state:
|
99 |
st.session_state.messages = []
|
100 |
|
101 |
+
st.title("DeepSeek μ±ν
")
|
102 |
+
st.markdown("DeepSeek AI λͺ¨λΈκ³Ό λννμΈμ. νμν κ²½μ° νμΌμ μ
λ‘λν μ μμ΅λλ€.")
|
|
|
103 |
|
|
|
104 |
with st.sidebar:
|
105 |
+
st.header("μ€μ ")
|
106 |
system_message = st.text_area(
|
107 |
+
"μμ€ν
λ©μμ§",
|
108 |
+
value="λΉμ μ κΉμ΄ μκ² μκ°νλ AIμ
λλ€. λ¬Έμ λ₯Ό κΉμ΄ κ³ λ €νκ³ μ²΄κ³μ μΈ μΆλ‘ κ³Όμ μ ν΅ν΄ μ¬λ°λ₯Έ ν΄κ²°μ±
μ λμΆνκΈ° μν΄ λ§€μ° κΈ΄ μ¬κ³ 체μΈμ μ¬μ©ν μ μμ΅λλ€. λ°λμ νκΈλ‘ λ΅λ³νμΈμ.",
|
109 |
height=100
|
110 |
)
|
111 |
+
max_tokens = st.slider("μ΅λ ν ν° μ", 1, 8192, 8192)
|
112 |
+
temperature = st.slider("μ¨λ", 0.1, 4.0, 0.0, 0.1)
|
113 |
+
top_p = st.slider("Top-p", 0.1, 1.0, 0.95, 0.05)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
uploaded_file = st.file_uploader(
|
115 |
+
"νμΌ μ
λ‘λ (μ νμ¬ν)",
|
116 |
+
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
|
|
|
|
|
117 |
accept_multiple_files=True
|
118 |
)
|
119 |
|
|
|
120 |
for message in st.session_state.messages:
|
121 |
with st.chat_message(message["role"]):
|
122 |
st.write(message["content"])
|
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.write(prompt)
|
128 |
|
|
|
129 |
with st.chat_message("assistant"):
|
130 |
response_placeholder = st.empty()
|
131 |
full_response = ""
|
132 |
|
|
|
133 |
history = [(msg["content"], next_msg["content"])
|
134 |
for msg, next_msg in zip(st.session_state.messages[::2], st.session_state.messages[1::2])]
|
135 |
|
|
|
136 |
for response_chunk in generate_response(
|
137 |
prompt,
|
138 |
history,
|
|
|
143 |
uploaded_file
|
144 |
):
|
145 |
full_response += response_chunk
|
|
|
146 |
response_placeholder.markdown(full_response + "β")
|
147 |
|
148 |
response_placeholder.markdown(full_response)
|
149 |
|
|
|
150 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
151 |
|
152 |
if __name__ == "__main__":
|