File size: 4,158 Bytes
63af1bf
3139717
 
f41ca4b
a2b114d
a5ae17c
63af1bf
 
 
56dfa80
63af1bf
 
8077043
63af1bf
f41ca4b
a2b114d
f41ca4b
 
 
 
 
 
a2b114d
f8d85a2
a2b114d
 
f41ca4b
a5ae17c
a2b114d
 
f41ca4b
 
a2b114d
 
 
 
 
 
 
63af1bf
 
19d8cf8
63af1bf
 
f41ca4b
 
63af1bf
 
f8d85a2
3139717
 
 
 
 
63af1bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3139717
 
 
63af1bf
 
8077043
3139717
 
63af1bf
3139717
 
8077043
3139717
a2b114d
3139717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63af1bf
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)}")