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