Spaces:
Sleeping
Sleeping
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)}") | |