Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from typing import Iterator
|
|
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:
|
@@ -31,22 +31,22 @@ def process_file(file) -> str:
|
|
31 |
else:
|
32 |
return file.getvalue().decode('utf-8')
|
33 |
except Exception as e:
|
34 |
-
st.error(f"
|
35 |
return ""
|
36 |
|
37 |
def format_message(role: str, content: str) -> dict:
|
38 |
-
"""
|
39 |
return {
|
40 |
"role": role,
|
41 |
"content": content
|
42 |
}
|
43 |
|
44 |
def get_formatted_history(messages: list) -> list:
|
45 |
-
"""
|
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"
|
@@ -65,23 +65,23 @@ def generate_response(
|
|
65 |
client = get_client()
|
66 |
|
67 |
try:
|
68 |
-
#
|
69 |
messages = []
|
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 |
|
78 |
-
#
|
79 |
if files:
|
80 |
file_contents = []
|
81 |
for file in files:
|
82 |
content = process_file(file)
|
83 |
if content:
|
84 |
-
file_contents.append(f"
|
85 |
if file_contents:
|
86 |
if formatted_history and formatted_history[-1]["role"] == "user":
|
87 |
formatted_history[-1]["content"] += "\n\n" + "\n\n".join(file_contents)
|
@@ -90,10 +90,10 @@ def generate_response(
|
|
90 |
|
91 |
messages.extend(formatted_history)
|
92 |
|
93 |
-
#
|
94 |
-
st.write("API
|
95 |
|
96 |
-
# API
|
97 |
try:
|
98 |
stream = client.chat.completions.create(
|
99 |
model="deepseek-ai/DeepSeek-R1",
|
@@ -110,61 +110,62 @@ def generate_response(
|
|
110 |
|
111 |
except Exception as e:
|
112 |
if "rate limit" in str(e).lower():
|
113 |
-
yield "API
|
114 |
else:
|
115 |
-
st.error(f"API
|
116 |
-
yield "
|
117 |
|
118 |
except Exception as e:
|
119 |
-
st.error(f"
|
120 |
-
yield "
|
121 |
|
122 |
def main():
|
123 |
-
st.set_page_config(page_title="DeepSeek
|
124 |
|
125 |
-
#
|
126 |
if "messages" not in st.session_state:
|
127 |
st.session_state.messages = []
|
128 |
|
129 |
-
st.title("DeepSeek
|
130 |
-
st.markdown("DeepSeek AI
|
131 |
|
132 |
with st.sidebar:
|
133 |
-
st.header("
|
134 |
system_message = st.text_area(
|
135 |
-
"
|
136 |
-
value="
|
137 |
height=100
|
138 |
)
|
139 |
-
max_tokens = st.slider("
|
140 |
-
temperature = st.slider("
|
141 |
top_p = st.slider("Top-p", 0.0, 1.0, 0.7, 0.1)
|
142 |
uploaded_file = st.file_uploader(
|
143 |
-
"
|
144 |
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
|
145 |
accept_multiple_files=True
|
146 |
)
|
|
|
147 |
|
148 |
-
#
|
149 |
for message in st.session_state.messages:
|
150 |
with st.chat_message(message["role"]):
|
151 |
st.markdown(message["content"])
|
152 |
|
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)
|
161 |
|
162 |
-
#
|
163 |
with st.chat_message("assistant"):
|
164 |
response_placeholder = st.empty()
|
165 |
full_response = ""
|
166 |
|
167 |
-
# generate_response
|
168 |
for response_chunk in generate_response(
|
169 |
prompt,
|
170 |
st.session_state.messages,
|
@@ -179,7 +180,7 @@ def main():
|
|
179 |
|
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 |
|
|
|
5 |
from PIL import Image
|
6 |
import base64
|
7 |
from PyPDF2 import PdfReader
|
8 |
+
import json # For debugging
|
9 |
|
10 |
API_KEY = os.getenv("TOGETHER_API_KEY")
|
11 |
if not API_KEY:
|
|
|
31 |
else:
|
32 |
return file.getvalue().decode('utf-8')
|
33 |
except Exception as e:
|
34 |
+
st.error(f"Error processing file: {str(e)}")
|
35 |
return ""
|
36 |
|
37 |
def format_message(role: str, content: str) -> dict:
|
38 |
+
"""Format message according to the API message format."""
|
39 |
return {
|
40 |
"role": role,
|
41 |
"content": content
|
42 |
}
|
43 |
|
44 |
def get_formatted_history(messages: list) -> list:
|
45 |
+
"""Convert conversation history to the API message format."""
|
46 |
formatted_messages = []
|
47 |
for msg in messages:
|
48 |
if isinstance(msg, dict) and "role" in msg and "content" in msg:
|
49 |
+
# Verify and correct the role if necessary
|
50 |
role = msg["role"]
|
51 |
if role not in ["system", "user", "assistant"]:
|
52 |
role = "user" if role == "human" else "assistant"
|
|
|
65 |
client = get_client()
|
66 |
|
67 |
try:
|
68 |
+
# Initialize message list
|
69 |
messages = []
|
70 |
|
71 |
+
# Add system message
|
72 |
if system_message.strip():
|
73 |
messages.append(format_message("system", system_message))
|
74 |
|
75 |
+
# Add conversation history (user messages already included)
|
76 |
formatted_history = get_formatted_history(history)
|
77 |
|
78 |
+
# If file content exists, add it to the last user message
|
79 |
if files:
|
80 |
file_contents = []
|
81 |
for file in files:
|
82 |
content = process_file(file)
|
83 |
if content:
|
84 |
+
file_contents.append(f"File content:\n{content}")
|
85 |
if file_contents:
|
86 |
if formatted_history and formatted_history[-1]["role"] == "user":
|
87 |
formatted_history[-1]["content"] += "\n\n" + "\n\n".join(file_contents)
|
|
|
90 |
|
91 |
messages.extend(formatted_history)
|
92 |
|
93 |
+
# Debug: Display API request messages
|
94 |
+
st.write("API Request Messages:", json.dumps(messages, ensure_ascii=False, indent=2))
|
95 |
|
96 |
+
# API Request
|
97 |
try:
|
98 |
stream = client.chat.completions.create(
|
99 |
model="deepseek-ai/DeepSeek-R1",
|
|
|
110 |
|
111 |
except Exception as e:
|
112 |
if "rate limit" in str(e).lower():
|
113 |
+
yield "API call rate limit reached. Please try again later."
|
114 |
else:
|
115 |
+
st.error(f"Detailed API error: {str(e)}")
|
116 |
+
yield "Sorry, please try again later."
|
117 |
|
118 |
except Exception as e:
|
119 |
+
st.error(f"Detailed error: {str(e)}")
|
120 |
+
yield "An error occurred, please try again later."
|
121 |
|
122 |
def main():
|
123 |
+
st.set_page_config(page_title="DeepSeek Chat", page_icon="π", layout="wide")
|
124 |
|
125 |
+
# Initialize session state
|
126 |
if "messages" not in st.session_state:
|
127 |
st.session_state.messages = []
|
128 |
|
129 |
+
st.title("DeepSeek Chat")
|
130 |
+
st.markdown("Chat with the DeepSeek AI model. You can upload files if needed.")
|
131 |
|
132 |
with st.sidebar:
|
133 |
+
st.header("Settings")
|
134 |
system_message = st.text_area(
|
135 |
+
"System Message",
|
136 |
+
value="You are a deeply thoughtful AI. Consider problems thoroughly and derive correct solutions through systematic reasoning. Please answer in English.",
|
137 |
height=100
|
138 |
)
|
139 |
+
max_tokens = st.slider("Max Tokens", 1, 4096, 2048)
|
140 |
+
temperature = st.slider("Temperature", 0.0, 2.0, 0.7, 0.1)
|
141 |
top_p = st.slider("Top-p", 0.0, 1.0, 0.7, 0.1)
|
142 |
uploaded_file = st.file_uploader(
|
143 |
+
"File Upload (Optional)",
|
144 |
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
|
145 |
accept_multiple_files=True
|
146 |
)
|
147 |
+
st.markdown("Join our Discord community: [https://discord.gg/openfreeai](https://discord.gg/openfreeai)")
|
148 |
|
149 |
+
# Display saved conversation messages
|
150 |
for message in st.session_state.messages:
|
151 |
with st.chat_message(message["role"]):
|
152 |
st.markdown(message["content"])
|
153 |
|
154 |
+
# Chat input
|
155 |
+
if prompt := st.chat_input("What would you like to know?"):
|
156 |
+
# Add user message
|
157 |
user_message = format_message("user", prompt)
|
158 |
st.session_state.messages.append(user_message)
|
159 |
|
160 |
with st.chat_message("user"):
|
161 |
st.markdown(prompt)
|
162 |
|
163 |
+
# Generate assistant response
|
164 |
with st.chat_message("assistant"):
|
165 |
response_placeholder = st.empty()
|
166 |
full_response = ""
|
167 |
|
168 |
+
# Call generate_response
|
169 |
for response_chunk in generate_response(
|
170 |
prompt,
|
171 |
st.session_state.messages,
|
|
|
180 |
|
181 |
response_placeholder.markdown(full_response)
|
182 |
|
183 |
+
# Save assistant response
|
184 |
assistant_message = format_message("assistant", full_response)
|
185 |
st.session_state.messages.append(assistant_message)
|
186 |
|