Spaces:
Paused
Paused
| import os | |
| import time | |
| import streamlit as st | |
| import google.generativeai as genai | |
| from streamlit_extras.colored_header import colored_header | |
| from streamlit_extras.add_vertical_space import add_vertical_space | |
| # Google Gemini API Key ์ค์ | |
| genai.configure(api_key=os.environ["GEMINI_API_KEY"]) | |
| # ๋ชจ๋ธ ์ค์ | |
| generation_config = { | |
| "temperature": 1, | |
| "top_p": 0.95, | |
| "top_k": 40, | |
| "max_output_tokens": 8192, | |
| "response_mime_type": "text/plain", | |
| } | |
| model = genai.GenerativeModel( | |
| model_name="gemini-2.0-flash", | |
| generation_config=generation_config, | |
| ) | |
| def generate_research_plan(question): | |
| """ | |
| ์ง๋ฌธ์ด๋ ํ๊ตฌ์ฃผ์ ๋ฅผ ์ ๋ ฅ๋ฐ์ ํตํฉํ๊ตฌ ๊ณํ์ ์์ฑํ๋ ํจ์ | |
| """ | |
| # ์ ์ฒด ํ๋กฌํํธ ์์ฑ | |
| prompt = f"""ํ์๋ค์ด ์ง๋ฌธ์ด๋ ํ๊ตฌ์ฃผ์ ๋ฅผ ์ ๋ ฅํ๋ฉด ๊ณผํ์๋ก์ ํตํฉํ๊ตฌ์ ์ ์ฐจ์ ๋ฐ๋ผ ์คํ์ ๊ตฌ์ฒด์ , ๋จ๊ณ์ , ์ฒด๊ณ์ ์ผ๋ก ์ค๊ณํ๊ณ ์์๋๋ ๊ฒฐ๊ณผ๋ฅผ ์ค๋ช ํด์ค. | |
| ์ง๋ฌธ ๋ฐ ํ๊ตฌ ์ฃผ์ : {question} | |
| ํตํฉํ๊ตฌ ๊ณผ์ : | |
| 1. ๋ฌธ์ ์ธ์: | |
| 2. ๊ฐ์ค ์ค์ : | |
| 3. ๋ณ์ธ ํต์ : | |
| 4. ์๋ฃ ํด์ ๋ฐฉ๋ฒ ๋ฐ ์ ์ฐจ: | |
| 5. ๊ฒฐ๋ก ๋์ถ ๋ฐฉ๋ฒ ๋ฐ ์ ์ฐจ: | |
| 6. ์์๋๋ ์ผ๋ฐํ: | |
| 7. ํ๊ตฌ๋ฅผ ์ํ ํ:""" | |
| # ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ์๋ต ์์ฑ | |
| response = model.generate_content([prompt], stream=True) | |
| collected_text = "" | |
| for token in response: | |
| chunk = token.text | |
| collected_text += chunk | |
| yield collected_text # ์ค์๊ฐ ์ถ๋ ฅ์ ์ํด yield ์ฌ์ฉ | |
| time.sleep(0.05) # ์ถ๋ ฅ ์๋ ์กฐ์ | |
| # Streamlit Interface | |
| colored_header( | |
| label="ํตํฉํ๊ตฌ ๊ณํ ์์ฑ๊ธฐ", | |
| description="๊ณผํ ํ๊ตฌ ์ฃผ์ ์ ๋ํ ์์ธํ ๊ณํ์ ์ธ์๋ณด์ธ์!", | |
| color_name="blue-70", | |
| ) | |
| add_vertical_space(1) | |
| question = st.text_area("์ง๋ฌธ์ด๋ ํ๊ตฌ์ฃผ์ ๋ฅผ ์ ๋ ฅํ์ธ์", height=100) | |
| generate_button = st.button("ํตํฉํ๊ตฌ ๊ณํ ์์ฑ") | |
| # ์ถ๋ ฅ ์์ญ ์ ์ | |
| output_area = st.empty() | |
| if generate_button and question: | |
| output_text = "" | |
| output_area.markdown(output_text) | |
| try: | |
| for partial_output in generate_research_plan(question): | |
| output_text = partial_output | |
| output_area.markdown(output_text) | |
| except Exception as e: | |
| st.error(f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}") | |
| # ๋ณต์ฌ ๋ฒํผ ์ถ๊ฐ | |
| if output_text: | |
| if st.button("์ถ๋ ฅ ๋ด์ฉ ๋ณต์ฌ"): | |
| st.write(output_text) | |
| st.success("์ถ๋ ฅ ๋ด์ฉ์ด ๋ณต์ฌ๋์์ต๋๋ค!") | |