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)}")