jonghhhh commited on
Commit
98ec3f6
Β·
verified Β·
1 Parent(s): ddeb95e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +79 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,81 @@
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
  import streamlit as st
2
+ import google.generativeai as genai
3
+ import os
4
+ import json
5
+ from io import StringIO
6
+
7
+ # ------------------------------------------------------
8
+ # Hugging Face Spacesμ—μ„œλŠ” ν™˜κ²½λ³€μˆ˜(Secrets)둜 API ν‚€λ₯Ό μ €μž₯ν•˜μ„Έμš”.
9
+ # Settings β†’ Repository secrets β†’ GEMINI_API_KEY 등둝
10
+ # ------------------------------------------------------
11
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
12
+ genai.configure(api_key=GOOGLE_API_KEY)
13
+
14
+ # 초기 μ„€μ •
15
+ if "messages" not in st.session_state:
16
+ st.session_state.messages = []
17
+
18
+ if "system_prompt" not in st.session_state:
19
+ st.session_state.system_prompt = "당신은 μΉœμ ˆν•œ AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€."
20
+
21
+ model = genai.GenerativeModel("gemma-3-27b-it")
22
+
23
+ # ------------------------------------------------------
24
+ # Streamlit UI
25
+ # ------------------------------------------------------
26
+ st.set_page_config(page_title="Gemini 챗봇", page_icon="πŸ€–", layout="wide")
27
+ st.title("πŸ€– Google Gemini λŒ€ν™”ν˜• 챗봇")
28
+ st.caption("Hugging Face Spaces + Streamlit + Google Generative AI")
29
+
30
+ # μ‚¬μ΄λ“œλ°”: μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μˆ˜μ • & 둜그 λ‹€μš΄λ‘œλ“œ
31
+ with st.sidebar:
32
+ st.subheader("βš™οΈ μ„€μ •")
33
+ new_system_prompt = st.text_area("μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ", st.session_state.system_prompt, height=100)
34
+ if st.button("λ³€κ²½ 적용"):
35
+ st.session_state.system_prompt = new_system_prompt
36
+ st.success("μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈκ°€ λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
37
+
38
+ st.markdown("---")
39
+ if st.session_state.messages:
40
+ # JSON λ³€ν™˜
41
+ json_data = json.dumps(st.session_state.messages, ensure_ascii=False, indent=2)
42
+ st.download_button(
43
+ label="πŸ’Ύ λŒ€ν™” 둜그 JSON λ‹€μš΄λ‘œλ“œ",
44
+ data=json_data,
45
+ file_name="chat_log.json",
46
+ mime="application/json"
47
+ )
48
+
49
+ # λŒ€ν™” 좜λ ₯
50
+ for msg in st.session_state.messages:
51
+ with st.chat_message("user"):
52
+ st.markdown(msg["user"])
53
+ with st.chat_message("assistant"):
54
+ st.markdown(msg["ai"])
55
+
56
+ # μ‚¬μš©μž μž…λ ₯
57
+ if prompt := st.chat_input("λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”. μ’…λ£Œν•˜λ €λ©΄ 'quit' μž…λ ₯"):
58
+ if prompt.lower() == "quit":
59
+ st.warning("λŒ€ν™”λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€. μƒˆλ‘œκ³ μΉ¨ν•˜λ©΄ λ‹€μ‹œ μ‹œμž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.")
60
+ else:
61
+ # λŒ€ν™” μ»¨ν…μŠ€νŠΈ ꡬ성
62
+ context = f"μ‹œμŠ€ν…œ: {st.session_state.system_prompt}\n"
63
+ for msg in st.session_state.messages[-10:]: # 졜근 10개만 μœ μ§€
64
+ context += f"μ‚¬μš©μž: {msg['user']}\nAI: {msg['ai']}\n"
65
+ context += f"μ‚¬μš©μž: {prompt}\nAI:"
66
+
67
+ with st.chat_message("user"):
68
+ st.markdown(prompt)
69
+
70
+ try:
71
+ response = model.generate_content(context)
72
+ ai_response = response.text
73
+ except Exception as e:
74
+ ai_response = f"⚠️ 였λ₯˜ λ°œμƒ: {e}"
75
+
76
+ with st.chat_message("assistant"):
77
+ st.markdown(ai_response)
78
+
79
+ # λŒ€ν™” 기둝 μ €μž₯
80
+ st.session_state.messages.append({"user": prompt, "ai": ai_response})
81