jonghhhh commited on
Commit
5b1b9ad
Β·
verified Β·
1 Parent(s): 35e8802

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +80 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,82 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
1
+ # streamlit_app.py
2
+
 
3
  import streamlit as st
4
+ import google.generativeai as genai
5
+ import os
6
+ import json
7
+ from io import StringIO
8
+
9
+ # ------------------------------------------------------
10
+ # Hugging Face Spacesμ—μ„œλŠ” ν™˜κ²½λ³€μˆ˜(Secrets)둜 API ν‚€λ₯Ό μ €μž₯
11
+ # Settings β†’ secrets β†’ New secret: GEMINI_API_KEY 등둝
12
+ # ------------------------------------------------------
13
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
14
+ genai.configure(api_key=GOOGLE_API_KEY)
15
+
16
+ # 초기 μ„€μ •
17
+ if "messages" not in st.session_state:
18
+ st.session_state.messages = []
19
+
20
+ if "system_prompt" not in st.session_state:
21
+ st.session_state.system_prompt = "당신은 μΉœμ ˆν•œ AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
22
+
23
+ model = genai.GenerativeModel("gemma-3-27b-it")
24
+
25
+ # ------------------------------------------------------
26
+ # Streamlit UI
27
+ # ------------------------------------------------------
28
+ st.set_page_config(page_title="Gemini 챗봇", page_icon="πŸ€–", layout="wide")
29
+ st.title("πŸ€– Google Gemini λŒ€ν™”ν˜• 챗봇")
30
+ st.caption("Hugging Face Spaces + Streamlit + Google Generative AI")
31
+
32
+ # μ‚¬μ΄λ“œλ°”: μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μˆ˜μ • & 둜그 λ‹€μš΄λ‘œλ“œ
33
+ with st.sidebar:
34
+ st.subheader("βš™οΈ μ„€μ •")
35
+ new_system_prompt = st.text_area("μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ", st.session_state.system_prompt, height=100)
36
+ if st.button("λ³€κ²½ 적용"):
37
+ st.session_state.system_prompt = new_system_prompt
38
+ st.success("μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈκ°€ λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
39
+
40
+ st.markdown("---")
41
+ if st.session_state.messages:
42
+ # JSON λ³€ν™˜
43
+ json_data = json.dumps(st.session_state.messages, ensure_ascii=False, indent=2)
44
+ st.download_button(
45
+ label="πŸ’Ύ λŒ€ν™” 둜그 JSON λ‹€μš΄λ‘œλ“œ",
46
+ data=json_data,
47
+ file_name="chat_log.json",
48
+ mime="application/json"
49
+ )
50
+
51
+ # λŒ€ν™” 좜λ ₯
52
+ for msg in st.session_state.messages:
53
+ with st.chat_message("user"):
54
+ st.markdown(msg["user"])
55
+ with st.chat_message("assistant"):
56
+ st.markdown(msg["ai"])
57
+
58
+ # μ‚¬μš©μž μž…λ ₯
59
+ if prompt := st.chat_input("λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”. μ’…λ£Œν•˜λ €λ©΄ 'quit' μž…λ ₯"):
60
+ if prompt.lower() == "quit":
61
+ st.warning("λŒ€ν™”λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€. μƒˆλ‘œκ³ μΉ¨ν•˜λ©΄ λ‹€μ‹œ μ‹œμž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.")
62
+ else:
63
+ # λŒ€ν™” μ»¨ν…μŠ€νŠΈ ꡬ성
64
+ context = f"μ‹œμŠ€ν…œ: {st.session_state.system_prompt}\n"
65
+ for msg in st.session_state.messages[-10:]: # 졜근 10개만 μœ μ§€
66
+ context += f"μ‚¬μš©μž: {msg['user']}\nAI: {msg['ai']}\n"
67
+ context += f"μ‚¬μš©μž: {prompt}\nAI:"
68
+
69
+ with st.chat_message("user"):
70
+ st.markdown(prompt)
71
+
72
+ try:
73
+ response = model.generate_content(context)
74
+ ai_response = response.text
75
+ except Exception as e:
76
+ ai_response = f"⚠️ 였λ₯˜ λ°œμƒ: {e}"
77
+
78
+ with st.chat_message("assistant"):
79
+ st.markdown(ai_response)
80
 
81
+ # λŒ€ν™” 기둝 μ €μž₯
82
+ st.session_state.messages.append({"user": prompt, "ai": ai_response})