Spaces:
Sleeping
Sleeping
File size: 5,738 Bytes
647040c |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import gradio as gr
import google.generativeai as genai
from transformers import pipeline
import os
from typing import IO # ํ์ผ ํ์
ํํธ๋ฅผ ์ํด ์ถ๊ฐ
# Hugging Face Spaces์ ๋ฉ๋ชจ๋ฆฌ ์ ํ์ ๊ณ ๋ คํ์ฌ ๊ฒฝ๋ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
# ํ๊ตญ์ด ์์ฝ์ ํนํ๋ ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค.
try:
summarizer = pipeline("summarization", model="gogamza/kobart-summarization")
except Exception as e:
print(f"๋ชจ๋ธ ๋ก๋ฉ ์ค ์ค๋ฅ ๋ฐ์: {e}")
summarizer = None
# Gradio 4.x์ ๋ง์ถฐ ํจ์ ์๊ทธ๋์ฒ๋ฅผ ์์ ํฉ๋๋ค.
# progress ์ธ์๋ฅผ ํ์
ํํธ๋ก ๋ฐ์ต๋๋ค.
def generate_blog_post(api_key: str, file1: IO, file2: IO, file3: IO, progress: gr.Progress):
"""
3๊ฐ์ ํ
์คํธ ํ์ผ์ ์
๋ ฅ๋ฐ์ ์์ฝํ๊ณ , Gemini API๋ฅผ ํตํด ๋ธ๋ก๊ทธ ๊ธ์ ์์ฑํ๋ ํจ์
"""
# 1. ์
๋ ฅ ๊ฐ ์ ํจ์ฑ ๊ฒ์ฌ
if not api_key:
raise gr.Error("Google API ํค๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.")
if not (file1 and file2 and file3):
raise gr.Error("3๊ฐ์ ํ
์คํธ ํ์ผ์ ๋ชจ๋ ์
๋ก๋ํด ์ฃผ์ธ์.")
if not summarizer:
raise gr.Error("์์ฝ ๋ชจ๋ธ์ ๋ก๋ํ๋ ๋ฐ ์คํจํ์ต๋๋ค. ์ฑ ๋ก๊ทธ๋ฅผ ํ์ธํด ์ฃผ์ธ์.")
try:
# 2. Google Gemini API ์ค์
progress(0.1, desc="Google API ํค ์ค์ ์ค...")
genai.configure(api_key=api_key)
# 3. ํ
์คํธ ํ์ผ ์ฝ๊ธฐ
progress(0.2, desc="ํ
์คํธ ํ์ผ ์ฝ๋ ์ค...")
texts = []
# Gradio 4.x์์ File ๊ฐ์ฒด๋ ์์ ํ์ผ ๊ฒฝ๋ก๋ฅผ name ์์ฑ์ผ๋ก ๊ฐ์ง๋๋ค.
files = [file1, file2, file3]
for f in files:
with open(f.name, 'r', encoding='utf-8') as file:
texts.append(file.read())
# 4. ํ๊น
ํ์ด์ค ๋ชจ๋ธ๋ก ๊ฐ ํ
์คํธ ์์ฝ
summaries = []
for i, text in enumerate(texts):
progress(0.3 + i * 0.1, desc=f"๋ฌธ์ {i+1} ์์ฝ ์ค...")
summary = summarizer(text, max_length=200, min_length=50, do_sample=False)
summaries.append(summary[0]['summary_text'])
# 5. Gemini API๋ก ์๋ก์ด ๊ธ ์์ฑ
progress(0.6, desc="Gemini API๋ก ๋ธ๋ก๊ทธ ์์๋ผ์ธ ์์ฑ ์ค...")
model = genai.GenerativeModel('gemini-1.5-flash-latest')
summaries_str = "\n".join([f"- ์์ฝ {i+1}: {s}" for i, s in enumerate(summaries)])
prompt = f"""
๋น์ ์ IT ์ ๋ฌธ ๋ค์ด๋ฒ ๋ธ๋ก๊ฑฐ์
๋๋ค. ์๋ 3๊ฐ์ ์์ฝ๋ฌธ์ ์ฐธ๊ณ ํ์ฌ, ๋
์๋ค์ ํฅ๋ฏธ๋ฅผ ๋ ์ ์๋ ๋ธ๋ก๊ทธ ๊ธ์ ์์ฑํด ์ฃผ์ธ์.
**์์ฑ ์กฐ๊ฑด:**
1. ๋จผ์ ๊ธ์ ์ ์ฒด์ ์ธ '์์๋ผ์ธ'์ ์๋ก , ๋ณธ๋ก , ๊ฒฐ๋ก ํ์์ผ๋ก ๋ช
ํํ๊ฒ ์ ์ํด ์ฃผ์ธ์.
2. ๊ทธ ๋ค์, ์์์ ์ ์ํ ์์๋ผ์ธ์ ๋ง์ถฐ์ ์ ์ฒด ๊ธ์ ์์ฑํด ์ฃผ์ธ์.
3. ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์คํ์ผ์ ๋ง๊ฒ ์น๊ทผํ๊ณ ์ดํดํ๊ธฐ ์ฌ์ด ์ดํฌ๋ฅผ ์ฌ์ฉํ๊ณ , ์ ์ ํ ์ด๋ชจ์ง๋ฅผ ์ฌ์ฉํด ์ฃผ์ธ์.
4. SEO๋ฅผ ๊ณ ๋ คํ์ฌ '์ธ๊ณต์ง๋ฅ', 'AI ๊ธฐ์ ', '๋ฏธ๋ ์ฌํ', 'ํ๊น
ํ์ด์ค', 'Gemini API'์ ๊ฐ์ ํค์๋๋ฅผ ๋ณธ๋ฌธ์ ์์ฐ์ค๋ฝ๊ฒ ํฌํจ์์ผ ์ฃผ์ธ์.
5. ์ ์ฒด ๊ธ์ ์๋ ๊ณต๋ฐฑ ํฌํจ 3,000์ ๋ด์ธ๋ก ๋ง์ถฐ์ฃผ์ธ์.
6. ๊ฒฐ๊ณผ๋ ๋งํฌ๋ค์ด ํ์์ผ๋ก ๊น๋ํ๊ฒ ์ ๋ฆฌํด์ ๋ณด์ฌ์ฃผ์ธ์.
**[์ฐธ๊ณ ํ ์์ฝ๋ฌธ]**
{summaries_str}
**[๊ฒฐ๊ณผ ์ถ๋ ฅ ํ์]**
# [๋ธ๋ก๊ทธ ์ ๋ชฉ]
## ๐ ์์๋ผ์ธ
- **์๋ก **: ...
- **๋ณธ๋ก **: ...
- **๊ฒฐ๋ก **: ...
---
## โ๏ธ ์์ฑ๋ ๋ธ๋ก๊ทธ ๊ธ
(์ฌ๊ธฐ์ ์์ฑ๋ ๊ธ์ ์ถ๋ ฅ)
"""
progress(0.8, desc="Gemini API๋ก ์ต์ข
๋ธ๋ก๊ทธ ๊ธ ์์ฑ ์ค...")
response = model.generate_content(prompt)
progress(1.0, desc="์๋ฃ!")
return response.text
except Exception as e:
raise gr.Error(f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {e}")
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# ๐ค AI ๊ธฐ๋ฐ ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ํฌ์คํ
์๋ ์์ฑ
3๊ฐ์ ํ
์คํธ ํ์ผ์ ์
๋ก๋ํ๊ณ Google API ํค๋ฅผ ์
๋ ฅํ๋ฉด, AI๊ฐ ํ์ผ ๋ด์ฉ์ ๋ถ์ํ๊ณ ์์ฝํ์ฌ ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ํ์์ ์๋ก์ด ๊ธ์ ๋ง๋ค์ด ๋๋ฆฝ๋๋ค.
"""
)
with gr.Row():
with gr.Column(scale=1):
api_key = gr.Textbox(
label="Google API ํค",
placeholder="์ฌ๊ธฐ์ Google API ํค๋ฅผ ์
๋ ฅํ์ธ์.",
type="password",
lines=1
)
file1 = gr.File(label="ํ
์คํธ ํ์ผ 1")
file2 = gr.File(label="ํ
์คํธ ํ์ผ 2")
file3 = gr.File(label="ํ
์คํธ ํ์ผ 3")
generate_btn = gr.Button("๐ ๋ธ๋ก๊ทธ ๊ธ ์์ฑํ๊ธฐ", variant="primary")
with gr.Column(scale=2):
output_text = gr.Markdown(label="โจ ์์ฑ๋ ๋ธ๋ก๊ทธ ๊ธ")
generate_btn.click(
fn=generate_blog_post,
inputs=[api_key, file1, file2, file3],
outputs=output_text
)
gr.Markdown(
"""
---
**์ฌ์ฉ ๋ฐฉ๋ฒ:**
1. [Google AI Studio](https://aistudio.google.com/app/apikey)์์ API ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์
๋ ฅํฉ๋๋ค.
2. ์ฃผ์ ๊ฐ ์ ์ฌํ 3๊ฐ์ ํ
์คํธ ํ์ผ(.txt)์ ๊ฐ๊ฐ ์
๋ก๋ํฉ๋๋ค.
3. '๋ธ๋ก๊ทธ ๊ธ ์์ฑํ๊ธฐ' ๋ฒํผ์ ํด๋ฆญํ๊ณ ์ ์ ๊ธฐ๋ค๋ฆฝ๋๋ค.
4. ์ค๋ฅธ์ชฝ ์ฐฝ์ ์์ฑ๋ ๊ธ์ ์์๋ผ์ธ๊ณผ ์ต์ข
๊ฒฐ๊ณผ๋ฌผ์ด ๋ํ๋ฉ๋๋ค.
"""
)
if __name__ == "__main__":
demo.launch()
|