deepseek_r1_API / app.py
fantos's picture
Update app.py
45e761b verified
raw
history blame
6.79 kB
import streamlit as st
from together import Together
import os
from typing import Iterator
from PIL import Image
import base64
from PyPDF2 import PdfReader
import json # λ””λ²„κΉ…μš© μΆ”κ°€
API_KEY = os.getenv("TOGETHER_API_KEY")
if not API_KEY:
raise ValueError("API key is missing! Make sure TOGETHER_API_KEY is set in the Secrets.")
@st.cache_resource
def get_client():
return Together(api_key=API_KEY)
def process_file(file) -> str:
if file is None:
return ""
try:
if file.type == "application/pdf":
text = ""
pdf_reader = PdfReader(file)
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
elif file.type.startswith("image/"):
return base64.b64encode(file.getvalue()).decode("utf-8")
else:
return file.getvalue().decode('utf-8')
except Exception as e:
st.error(f"파일 처리 쀑 였λ₯˜ λ°œμƒ: {str(e)}")
return ""
def format_message(role: str, content: str) -> dict:
"""API λ©”μ‹œμ§€ ν˜•μ‹μ— 맞게 λ©”μ‹œμ§€λ₯Ό ν¬λ§·νŒ…ν•©λ‹ˆλ‹€."""
return {
"role": role,
"content": content
}
def get_formatted_history(messages: list) -> list:
"""λŒ€ν™” νžˆμŠ€ν† λ¦¬λ₯Ό API ν˜•μ‹μ— 맞게 λ³€ν™˜ν•©λ‹ˆλ‹€."""
formatted_messages = []
for msg in messages:
if isinstance(msg, dict) and "role" in msg and "content" in msg:
# 역할이 μ˜¬λ°”λ₯Έμ§€ ν™•μΈν•˜κ³  μˆ˜μ •
role = msg["role"]
if role not in ["system", "user", "assistant"]:
role = "user" if role == "human" else "assistant"
formatted_messages.append(format_message(role, msg["content"]))
return formatted_messages
def generate_response(
message: str,
history: list,
system_message: str,
max_tokens: int,
temperature: float,
top_p: float,
files=None
) -> Iterator[str]:
client = get_client()
try:
# λ©”μ‹œμ§€ λ°°μ—΄ μ΄ˆκΈ°ν™”
messages = []
# μ‹œμŠ€ν…œ λ©”μ‹œμ§€ μΆ”κ°€
if system_message.strip():
messages.append(format_message("system", system_message))
# λŒ€ν™” νžˆμŠ€ν† λ¦¬ μΆ”κ°€ (이미 μ‚¬μš©μž λ©”μ‹œμ§€κ°€ ν¬ν•¨λ˜μ–΄ 있음)
formatted_history = get_formatted_history(history)
# 파일 λ‚΄μš©μ΄ μžˆλŠ” 경우 λ§ˆμ§€λ§‰ μ‚¬μš©μž λ©”μ‹œμ§€μ— μΆ”κ°€
if files:
file_contents = []
for file in files:
content = process_file(file)
if content:
file_contents.append(f"파일 λ‚΄μš©:\n{content}")
if file_contents:
if formatted_history and formatted_history[-1]["role"] == "user":
formatted_history[-1]["content"] += "\n\n" + "\n\n".join(file_contents)
else:
formatted_history.append(format_message("user", "\n\n".join(file_contents)))
messages.extend(formatted_history)
# 디버깅: API μš”μ²­ λ‚΄μš© 좜λ ₯
st.write("API μš”μ²­ λ©”μ‹œμ§€:", json.dumps(messages, ensure_ascii=False, indent=2))
# API μš”μ²­
try:
stream = client.chat.completions.create(
model="deepseek-ai/DeepSeek-R1",
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
stream=True
)
for chunk in stream:
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
except Exception as e:
if "rate limit" in str(e).lower():
yield "API 호좜 ν•œλ„μ— λ„λ‹¬ν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”."
else:
st.error(f"API 였λ₯˜ 상세: {str(e)}")
yield "μ£„μ†‘ν•©λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”."
except Exception as e:
st.error(f"전체 였λ₯˜ 상세: {str(e)}")
yield "였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”."
def main():
st.set_page_config(page_title="DeepSeek μ±„νŒ…", page_icon="πŸ’­", layout="wide")
# μ„Έμ…˜ μƒνƒœ μ΄ˆκΈ°ν™”
if "messages" not in st.session_state:
st.session_state.messages = []
st.title("DeepSeek μ±„νŒ…")
st.markdown("DeepSeek AI λͺ¨λΈκ³Ό λŒ€ν™”ν•˜μ„Έμš”. ν•„μš”ν•œ 경우 νŒŒμΌμ„ μ—…λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€.")
with st.sidebar:
st.header("μ„€μ •")
system_message = st.text_area(
"μ‹œμŠ€ν…œ λ©”μ‹œμ§€",
value="당신은 깊이 있게 μƒκ°ν•˜λŠ” AIμž…λ‹ˆλ‹€. 문제λ₯Ό 깊이 κ³ λ €ν•˜κ³  체계적인 μΆ”λ‘  과정을 톡해 μ˜¬λ°”λ₯Έ 해결책을 λ„μΆœν•˜μ„Έμš”. λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•˜μ„Έμš”.",
height=100
)
max_tokens = st.slider("μ΅œλŒ€ 토큰 수", 1, 4096, 2048)
temperature = st.slider("μ˜¨λ„", 0.0, 2.0, 0.7, 0.1)
top_p = st.slider("Top-p", 0.0, 1.0, 0.7, 0.1)
uploaded_file = st.file_uploader(
"파일 μ—…λ‘œλ“œ (선택사항)",
type=['txt', 'py', 'md', 'pdf', 'png', 'jpg', 'jpeg'],
accept_multiple_files=True
)
# μ €μž₯된 λŒ€ν™” λ©”μ‹œμ§€ ν‘œμ‹œ
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# μ±„νŒ… μž…λ ₯
if prompt := st.chat_input("무엇을 μ•Œκ³  μ‹ΆμœΌμ‹ κ°€μš”?"):
# μ‚¬μš©μž λ©”μ‹œμ§€ μΆ”κ°€ (ν•œλ²ˆλ§Œ μΆ”κ°€)
user_message = format_message("user", prompt)
st.session_state.messages.append(user_message)
with st.chat_message("user"):
st.markdown(prompt)
# μ–΄μ‹œμŠ€ν„΄νŠΈ 응닡 생성
with st.chat_message("assistant"):
response_placeholder = st.empty()
full_response = ""
# generate_response 호좜
for response_chunk in generate_response(
prompt,
st.session_state.messages,
system_message,
max_tokens,
temperature,
top_p,
uploaded_file
):
full_response += response_chunk
response_placeholder.markdown(full_response + "β–Œ")
response_placeholder.markdown(full_response)
# 응닡 μ €μž₯
assistant_message = format_message("assistant", full_response)
st.session_state.messages.append(assistant_message)
if __name__ == "__main__":
main()