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|
+
from gtts import gTTS
|
| 7 |
+
import tempfile
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# --- ์ค์ ์ฌํญ ---
|
| 12 |
+
# ํ๊น
ํ์ด์ค Secrets ๋๋ ์์คํ
ํ๊ฒฝ๋ณ์์์ API ํค๋ฅผ ์์ ํ๊ฒ ๋ถ๋ฌ์ต๋๋ค.
|
| 13 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 14 |
+
|
| 15 |
+
if not GEMINI_API_KEY:
|
| 16 |
+
raise ValueError("API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊น
ํ์ด์ค Settings -> Secrets์์ GEMINI_API_KEY๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.")
|
| 17 |
+
|
| 18 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 19 |
+
|
| 20 |
+
# ์๋ฅ ์ง๋ฌธ ์ถ์ฒ์ฉ RSS ํผ๋ ๋ชฉ๋ก
|
| 21 |
+
RSS_FEEDS = {
|
| 22 |
+
"Aeon": "https://aeon.co/feed.rss",
|
| 23 |
+
"Psyche": "https://psyche.co/feed",
|
| 24 |
+
"The Conversation": "https://theconversation.com/global/articles.atom",
|
| 25 |
+
"Scientific American": "https://rss.sciam.com/ScientificAmerican-Global",
|
| 26 |
+
"Nautilus": "https://nautil.us/feed/"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
def fetch_random_article():
|
| 30 |
+
"""RSS ํผ๋์์ ๋๋ค์ผ๋ก ๊ธฐ์ฌ URL๊ณผ ์ ๋ชฉ์ ๊ฐ์ ธ์ต๋๋ค."""
|
| 31 |
+
site_name, feed_url = random.choice(list(RSS_FEEDS.items()))
|
| 32 |
+
feed = feedparser.parse(feed_url)
|
| 33 |
+
|
| 34 |
+
if not feed.entries:
|
| 35 |
+
return None, None, site_name
|
| 36 |
+
|
| 37 |
+
random_entry = random.choice(feed.entries)
|
| 38 |
+
return random_entry.title, random_entry.link, site_name
|
| 39 |
+
|
| 40 |
+
def scrape_article_text(url):
|
| 41 |
+
"""URL์์ ๋ณธ๋ฌธ ํ
์คํธ๋ฅผ ํฌ๋กค๋งํฉ๋๋ค."""
|
| 42 |
+
try:
|
| 43 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 44 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 45 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 46 |
+
|
| 47 |
+
# p ํ๊ทธ ์์ฃผ๋ก ํ
์คํธ ์ถ์ถ (์ฌ์ดํธ๋ณ ๊ตฌ์กฐ๊ฐ ๋ค๋ฅด๋ฏ๋ก ๋ฒ์ฉ์ ์ธ ๋ฐฉ์ ์ฌ์ฉ)
|
| 48 |
+
paragraphs = soup.find_all('p')
|
| 49 |
+
text = ' '.join([p.get_text() for p in paragraphs])
|
| 50 |
+
|
| 51 |
+
# ํ
์คํธ๊ฐ ๋๋ฌด ๊ธธ๋ฉด ์๋ถ๋ถ๋ง ์๋ฆ (API ํ ํฐ ์ ํ ๋ฐ ์๋ฅ ์ง๋ฌธ ๋ถ๋ ๊ณ ๋ ค)
|
| 52 |
+
return text[:4000]
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return str(e)
|
| 55 |
+
|
| 56 |
+
def generate_tts(text):
|
| 57 |
+
"""์์ด ์ง๋ฌธ์ ์์ฑ(mp3)์ผ๋ก ๋ณํํฉ๋๋ค."""
|
| 58 |
+
try:
|
| 59 |
+
tts = gTTS(text=text, lang='en', slow=False)
|
| 60 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 61 |
+
tts.save(temp_file.name)
|
| 62 |
+
return temp_file.name
|
| 63 |
+
except Exception:
|
| 64 |
+
return None
|
| 65 |
+
|
| 66 |
+
def process_suneung_material():
|
| 67 |
+
"""์ ์ฒด ํ์ดํ๋ผ์ธ: ๊ธฐ์ฌ ์์ง -> AI ํ๋กฌํํธ ์์ฒญ -> TTS ์์ฑ -> ๊ฒฐ๊ณผ ๋ฐํ"""
|
| 68 |
+
title, link, site_name = fetch_random_article()
|
| 69 |
+
|
| 70 |
+
if not link:
|
| 71 |
+
return "๊ธฐ์ฌ๋ฅผ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ต๋๋ค.", "", "", "", None
|
| 72 |
+
|
| 73 |
+
raw_text = scrape_article_text(link)
|
| 74 |
+
|
| 75 |
+
if len(raw_text) < 200:
|
| 76 |
+
return "๊ธฐ์ฌ ๋ณธ๋ฌธ์ด ๋๋ฌด ์งง๊ฑฐ๋ ํฌ๋กค๋ง์ด ์ฐจ๋จ๋ ์ฌ์ดํธ์
๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์.", "", "", "", None
|
| 77 |
+
|
| 78 |
+
# ์๋ฅ ์ถ์ ์์ ๊ด์ ์ Gemini ํ๋กฌํํธ
|
| 79 |
+
prompt = f"""
|
| 80 |
+
You are an expert in creating English questions for the Korean CSAT (์๋ฅ ์์ด ์ถ์ ์์).
|
| 81 |
+
Read the following article excerpt and perform the tasks below.
|
| 82 |
+
|
| 83 |
+
[Source Material]
|
| 84 |
+
{raw_text}
|
| 85 |
+
|
| 86 |
+
[Tasks]
|
| 87 |
+
1. **CSAT Passage (์๋ฅํ ์ง๋ฌธ)**: Rewrite and summarize the core idea into a logically coherent passage of exactly 150-180 words. The vocabulary and sentence structure must match the high difficulty level of the Korean CSAT English section. DO NOT include the blank or altered sequence here; just provide the perfect base passage.
|
| 88 |
+
2. **Vocabulary (์ฃผ์ ์ดํ)**: Extract 7-10 high-level vocabulary words from your generated passage. Format strictly as: "English Word | Korean Meaning | Synonym".
|
| 89 |
+
3. **Fill-in-the-blank Question (๋น์นธ ์ถ๋ก ๋ฌธ์ )**: Create a CSAT-style fill-in-the-blank question using the passage. Replace a crucial thematic phrase with "________". Provide 5 multiple-choice options (1 correct, 4 highly attractive distractors).
|
| 90 |
+
4. **Sentence Ordering Question (๊ธ์ ์์ ๋ฌธ์ )**: Take the same passage, keep the first sentence as the given box, and divide the rest into three parts (A), (B), and (C) in a scrambled order. Provide the correct order.
|
| 91 |
+
|
| 92 |
+
Format the output cleanly with the following headers:
|
| 93 |
+
[์๋ฅํ ์ง๋ฌธ]
|
| 94 |
+
[์ฃผ์ ์ดํ]
|
| 95 |
+
[๋น์นธ ์ถ๋ก ๋ฌธ์ ]
|
| 96 |
+
[๊ธ์ ์์ ๋ฌธ์ ]
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# Gemini 1.5 Flash ๋๋ Pro ๋ชจ๋ธ ์ฌ์ฉ ๊ถ์ฅ
|
| 101 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 102 |
+
response = model.generate_content(prompt)
|
| 103 |
+
result_text = response.text
|
| 104 |
+
|
| 105 |
+
# ์์ฑ๋ ํ
์คํธ์์ '[์๋ฅํ ์ง๋ฌธ]' ๋ถ๋ถ๋ง ์ถ์ถํ์ฌ TTS ์์ฑ์ฉ์ผ๋ก ์ฌ์ฉ
|
| 106 |
+
passage_for_tts = ""
|
| 107 |
+
if "[์๋ฅํ ์ง๋ฌธ]" in result_text and "[์ฃผ์ ์ดํ]" in result_text:
|
| 108 |
+
passage_for_tts = result_text.split("[์ฃผ์ ์ดํ]")[0].replace("[์๋ฅํ ์ง๋ฌธ]", "").strip()
|
| 109 |
+
else:
|
| 110 |
+
passage_for_tts = "Could not parse passage for audio."
|
| 111 |
+
|
| 112 |
+
audio_path = generate_tts(passage_for_tts)
|
| 113 |
+
|
| 114 |
+
source_info = f"**์ถ์ฒ:** {site_name} - [{title}]({link})"
|
| 115 |
+
|
| 116 |
+
return result_text, source_info, audio_path
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"AI ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", None
|
| 120 |
+
|
| 121 |
+
# --- Gradio UI ๊ตฌ์ฑ ---
|
| 122 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 123 |
+
gr.Markdown("# ๐ ์๋ฅ ์์ด ๋ชจ์ ๋ฌธํญ ์๋ ์์ฑ๊ธฐ")
|
| 124 |
+
gr.Markdown("Nautilus, Scientific American, The Conversation, Psyche, Aeon์์ ๋๋ค์ผ๋ก ๊ธฐ์ฌ๋ฅผ ๋ฐ์ทํ์ฌ ์๋ฅํ ์ง๋ฌธ๊ณผ ๋ฌธํญ์ ์ ์ํฉ๋๋ค.")
|
| 125 |
+
|
| 126 |
+
with gr.Row():
|
| 127 |
+
generate_btn = gr.Button("๋๋ค ์ง๋ฌธ ๋ฐ ๋ฌธ์ ์์ฑํ๊ธฐ (Click)", variant="primary")
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column(scale=2):
|
| 131 |
+
output_content = gr.Markdown(label="์ถ์ ๋ด์ฉ")
|
| 132 |
+
source_display = gr.Markdown(label="์ง๋ฌธ ์ถ์ฒ")
|
| 133 |
+
|
| 134 |
+
with gr.Column(scale=1):
|
| 135 |
+
audio_player = gr.Audio(label="์ง๋ฌธ ๋ฃ๊ธฐ (TTS)", type="filepath", interactive=False)
|
| 136 |
+
|
| 137 |
+
# ๋ฒํผ ํด๋ฆญ ์ ํจ์ ์ฐ๊ฒฐ (๋ก๋ฉ ์ ๋๋ฉ์ด์
ํฌํจ)
|
| 138 |
+
generate_btn.click(
|
| 139 |
+
fn=process_suneung_material,
|
| 140 |
+
inputs=[],
|
| 141 |
+
outputs=[output_content, source_display, audio_player]
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo.launch()
|