Yescia's picture
Update app.py
f8d85a2 verified
import streamlit as st
import requests
import json
import io
import pandas as pd
from PyPDF2 import PdfReader
st.set_page_config(page_title="AI 사업 뢄석 λ„μš°λ―Έ", layout="wide")
st.title("πŸ€– 업무 μ†μ˜ AI: Solar 기반 사업 뢄석 λ„μš°λ―Έ")
# API ν‚€ μž…λ ₯
api_key = st.text_input("πŸ”‘ Upstage API Key μž…λ ₯ (up-둜 μ‹œμž‘)", type="password")
# μ‚¬μš©μž μž…λ ₯ 방식 선택
input_method = st.radio("πŸ“₯ 사업 λ‚΄μš© μž…λ ₯ 방식 선택:", ["ν…μŠ€νŠΈ 직접 μž…λ ₯", "파일 μ—…λ‘œλ“œ (txt/pdf/csv)"])
input_text = ""
if input_method == "ν…μŠ€νŠΈ 직접 μž…λ ₯":
input_text = st.text_area("πŸ’¬ λΆ„μ„ν•˜κ³  싢은 사업 λ‚΄μš©μ„ μž…λ ₯ν•˜μ„Έμš”:", height=300)
elif input_method == "파일 μ—…λ‘œλ“œ (txt/pdf/csv)":
uploaded_files = st.file_uploader("πŸ“Ž txt, pdf, csv νŒŒμΌμ„ μ—¬λŸ¬ 개 μ—…λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€", type=["txt", "pdf", "csv"], accept_multiple_files=True)
combined_texts = []
for uploaded_file in uploaded_files:
if uploaded_file.type == "application/pdf":
reader = PdfReader(uploaded_file)
text = "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
combined_texts.append(text)
elif uploaded_file.type == "text/plain":
stringio = io.StringIO(uploaded_file.getvalue().decode("utf-8"))
text = stringio.read()
combined_texts.append(text)
elif uploaded_file.type == "text/csv":
df = pd.read_csv(uploaded_file)
text = df.to_string(index=False)
combined_texts.append(text)
input_text = "\n\n".join(combined_texts)
# λ²„νŠΌ 클릭 μ‹œ μ‹€ν–‰
if st.button("πŸš€ Solarμ—κ²Œ 뢄석 μš”μ²­ν•˜κΈ°"):
if not api_key:
st.warning("API Keyλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.")
elif not input_text.strip():
st.warning("뢄석할 λ‚΄μš©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš” λ˜λŠ” νŒŒμΌμ„ μ—…λ‘œλ“œν•΄μ£Όμ„Έμš”.")
else:
try:
with st.spinner("Solarκ°€ 뢄석 μ€‘μž…λ‹ˆλ‹€...⏳"):
url = "https://api.upstage.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
prompt = f"""
λ‹€μŒμ€ μ‚¬μš©μžμ˜ 사업 μ„€λͺ…μž…λ‹ˆλ‹€:
{input_text}
이 λ‚΄μš©μ„ λ°”νƒ•μœΌλ‘œ μ•„λž˜ ν•­λͺ©μ„ κΈ°μ€€μœΌλ‘œ 뢄석해 μ£Όμ„Έμš”:
1. ν•΄λ‹Ή 사업이 ν•΄κ²°ν•˜κ³ μž ν•˜λŠ” λ¬Έμ œλŠ” λ¬΄μ—‡μΈκ°€μš”?
2. μ–΄λ–€ AI κΈ°μˆ μ„ ν™œμš©ν•˜κ³  μžˆλ‚˜μš”?
3. 기쑴의 방식보닀 μ–΄λ–€ 점이 κ°œμ„ λ˜μ—ˆλ‚˜μš”?
4. 이 AI μ„œλΉ„μŠ€μ˜ 핡심 κ°€μΉ˜λŠ” λ¬΄μ—‡μΈκ°€μš”?
5. ν–₯ν›„ ν™•μž₯ κ°€λŠ₯성이 μžˆλ‹€λ©΄ μ–΄λ–€ λ°©ν–₯이 μžˆμ„κΉŒμš”?
ν•œκ΅­μ–΄λ‘œ 정리해 μ£Όμ„Έμš”.
"""
data = {
"model": "solar-pro",
"messages": [
{"role": "system", "content": "당신은 λ›°μ–΄λ‚œ AI μ„œλΉ„μŠ€ λΆ„μ„κ°€μž…λ‹ˆλ‹€."},
{"role": "user", "content": prompt}
],
"stream": True
}
response = requests.post(url, headers=headers, json=data, stream=True)
response.raise_for_status()
st.success("βœ… 뢄석 슀트리밍 μ‹œμž‘!")
st.markdown("### πŸ“Œ Solar 뢄석 κ²°κ³Ό:")
result_placeholder = st.empty()
result_text = ""
for line in response.iter_lines():
if line:
try:
if line.startswith(b"data: "):
line = line.replace(b"data: ", b"")
data = json.loads(line.decode("utf-8"))
delta = data["choices"][0]["delta"].get("content")
if delta:
result_text += delta
result_placeholder.markdown(result_text)
except Exception:
continue
except Exception as e:
st.error(f"μ—λŸ¬ λ°œμƒ: {str(e)}")