Upload 4 files
Browse files- app.py +38 -140
- requirements.txt +5 -2
app.py
CHANGED
|
@@ -1,158 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
-
import requests
|
| 4 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
-
import io
|
| 6 |
-
import base64
|
| 7 |
from transformers import pipeline
|
| 8 |
-
from config import MEME_TEMPLATES, CHATBOT_RESPONSES, APP_CONFIG
|
| 9 |
-
from utils import create_meme_image, generate_response, add_text_to_image
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
| 14 |
-
text_generator = pipeline("text-generation", model="gpt2", max_length=100)
|
| 15 |
-
except Exception as e:
|
| 16 |
-
print(f"๋ชจ๋ธ ๋ก๋ฉ ์ค๋ฅ: {e}")
|
| 17 |
-
qa_pipeline = None
|
| 18 |
-
text_generator = None
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
"""๋ฐ ์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if not message.strip():
|
| 37 |
-
return "๋ฉ์์ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 38 |
-
|
| 39 |
-
# ๊ฐ๋จํ ํค์๋ ๊ธฐ๋ฐ ์๋ต
|
| 40 |
-
response = generate_response(message, qa_pipeline, text_generator)
|
| 41 |
-
return response
|
| 42 |
-
|
| 43 |
-
except Exception as e:
|
| 44 |
-
return f"์๋ต ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 45 |
-
|
| 46 |
-
def analyze_meme_trend(keyword):
|
| 47 |
-
"""๋ฐ ํธ๋ ๋ ๋ถ์ (์๋ฎฌ๋ ์ด์
)"""
|
| 48 |
-
trends = [
|
| 49 |
-
f"'{keyword}' ๊ด๋ จ ๋ฐ์ด ์ต๊ทผ {random.randint(10, 50)}% ์ฆ๊ฐํ์ต๋๋ค.",
|
| 50 |
-
f"'{keyword}' ๋ฐ์ ์ธ๊ธฐ๋: {random.randint(70, 95)}/100",
|
| 51 |
-
f"์ ์ฌํ ํค์๋: {', '.join(random.sample(['์ฌ๋ฏธ์๋', '์๊ธด', '์ธ๊ธฐ', '๋ฐ์ด๋ด', 'ํธ๋ ๋'], 3))}"
|
| 52 |
-
]
|
| 53 |
-
return "\n".join(trends)
|
| 54 |
-
|
| 55 |
-
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 56 |
-
with gr.Blocks(title=APP_CONFIG["title"], theme=gr.themes.Soft()) as demo:
|
| 57 |
-
gr.Markdown("# ๐ญ ๋ฐ ์์ฑ & AI ์ฑ๋ด ์๋น์ค")
|
| 58 |
-
gr.Markdown("๋ฐ์ ๋ง๋ค๊ณ , AI์ ๋ํํ๋ฉฐ, ํธ๋ ๋๋ฅผ ๋ถ์ํด๋ณด์ธ์!")
|
| 59 |
|
| 60 |
-
with gr.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
template_dropdown = gr.Dropdown(
|
| 66 |
-
choices=list(MEME_TEMPLATES.keys()),
|
| 67 |
-
label="๋ฐ ํ
ํ๋ฆฟ ์ ํ",
|
| 68 |
-
value=list(MEME_TEMPLATES.keys())[0]
|
| 69 |
-
)
|
| 70 |
-
top_text_input = gr.Textbox(
|
| 71 |
-
label="์๋จ ํ
์คํธ",
|
| 72 |
-
placeholder="์๋จ์ ๋ค์ด๊ฐ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์",
|
| 73 |
-
value="When you see a bug"
|
| 74 |
-
)
|
| 75 |
-
bottom_text_input = gr.Textbox(
|
| 76 |
-
label="ํ๋จ ํ
์คํธ",
|
| 77 |
-
placeholder="ํ๋จ์ ๋ค์ด๊ฐ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์",
|
| 78 |
-
value="But it works in production"
|
| 79 |
-
)
|
| 80 |
-
create_btn = gr.Button("๋ฐ ์์ฑํ๊ธฐ", variant="primary")
|
| 81 |
-
|
| 82 |
-
with gr.Column():
|
| 83 |
-
meme_output = gr.Image(label="์์ฑ๋ ๋ฐ")
|
| 84 |
-
|
| 85 |
-
create_btn.click(
|
| 86 |
-
fn=create_meme,
|
| 87 |
-
inputs=[template_dropdown, top_text_input, bottom_text_input],
|
| 88 |
-
outputs=meme_output
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
# AI ์ฑ๋ด ํญ
|
| 92 |
-
with gr.TabItem("๐ค AI ์ฑ๋ด"):
|
| 93 |
-
chatbot = gr.Chatbot(
|
| 94 |
-
label="AI ์ด์์คํดํธ",
|
| 95 |
-
placeholder="์๋
ํ์ธ์! ๋ฌด์์ ๋์๋๋ฆด๊น์?",
|
| 96 |
-
height=400
|
| 97 |
-
)
|
| 98 |
-
msg_input = gr.Textbox(
|
| 99 |
-
label="๋ฉ์์ง ์
๋ ฅ",
|
| 100 |
-
placeholder="๊ถ๊ธํ ๊ฒ์ ๋ฌผ์ด๋ณด์ธ์...",
|
| 101 |
lines=2
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
def user_message(message, history):
|
| 109 |
-
return "", history + [[message, None]]
|
| 110 |
-
|
| 111 |
-
def bot_message(history):
|
| 112 |
-
if history and history[-1][1] is None:
|
| 113 |
-
user_msg = history[-1][0]
|
| 114 |
-
bot_response = chatbot_response(user_msg, history)
|
| 115 |
-
history[-1][1] = bot_response
|
| 116 |
-
return history
|
| 117 |
-
|
| 118 |
-
msg_input.submit(user_message, [msg_input, chatbot], [msg_input, chatbot]).then(
|
| 119 |
-
bot_message, chatbot, chatbot
|
| 120 |
)
|
| 121 |
-
|
| 122 |
-
bot_message, chatbot, chatbot
|
| 123 |
-
)
|
| 124 |
-
clear_btn.click(lambda: [], outputs=chatbot)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
label="ํค์๋ ์
๋ ฅ",
|
| 132 |
-
placeholder="๋ถ์ํ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์",
|
| 133 |
-
value="AI"
|
| 134 |
-
)
|
| 135 |
-
analyze_btn = gr.Button("ํธ๋ ๋ ๋ถ์", variant="primary")
|
| 136 |
-
|
| 137 |
-
with gr.Column():
|
| 138 |
-
trend_output = gr.Textbox(
|
| 139 |
-
label="๋ถ์ ๊ฒฐ๊ณผ",
|
| 140 |
-
lines=5,
|
| 141 |
-
interactive=False
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
analyze_btn.click(
|
| 145 |
-
fn=analyze_meme_trend,
|
| 146 |
-
inputs=keyword_input,
|
| 147 |
-
outputs=trend_output
|
| 148 |
)
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
|
|
|
|
| 153 |
if __name__ == "__main__":
|
| 154 |
demo.launch(
|
| 155 |
-
server_name="0.0.0.0",
|
| 156 |
-
server_port=7860,
|
| 157 |
-
share=False
|
| 158 |
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 5 |
+
qna = pipeline("question-answering")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def answer_question(question, context):
|
|
|
|
| 8 |
try:
|
| 9 |
+
result = qna({
|
| 10 |
+
"question": question,
|
| 11 |
+
"context": context
|
| 12 |
+
})
|
| 13 |
+
if isinstance(result, list):
|
| 14 |
+
return result[0]['answer']
|
| 15 |
+
return result['answer']
|
| 16 |
except Exception as e:
|
| 17 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 18 |
|
| 19 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("# ์ง์์๋ต ์ฑ๋ด")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
with gr.Row():
|
| 24 |
+
with gr.Column():
|
| 25 |
+
question_input = gr.Textbox(
|
| 26 |
+
label="์ง๋ฌธ",
|
| 27 |
+
placeholder="์ง๋ฌธ์ ์
๋ ฅํ์ธ์",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
lines=2
|
| 29 |
)
|
| 30 |
+
context_input = gr.Textbox(
|
| 31 |
+
label="๋ฌธ๋งฅ",
|
| 32 |
+
placeholder="๊ด๋ จ ๋ฌธ๋งฅ์ ์
๋ ฅํ์ธ์",
|
| 33 |
+
lines=5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
+
submit_btn = gr.Button("๋ต๋ณ ์์ฑ", variant="primary")
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
with gr.Column():
|
| 38 |
+
answer_output = gr.Textbox(
|
| 39 |
+
label="๋ต๋ณ",
|
| 40 |
+
lines=3,
|
| 41 |
+
interactive=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
+
submit_btn.click(
|
| 45 |
+
fn=answer_question,
|
| 46 |
+
inputs=[question_input, context_input],
|
| 47 |
+
outputs=answer_output
|
| 48 |
+
)
|
| 49 |
|
| 50 |
+
# ์ค์: Spaces ๋ฐฐํฌ์ ์ด ๋ถ๋ถ์ด ํต์ฌ
|
| 51 |
if __name__ == "__main__":
|
| 52 |
demo.launch(
|
| 53 |
+
server_name="0.0.0.0", # ๋ชจ๋ IP์์ ์ ๊ทผ ๊ฐ๋ฅ
|
| 54 |
+
server_port=7860, # ๊ธฐ๋ณธ ํฌํธ
|
| 55 |
+
share=False # Spaces์์๋ False ์ฌ์ฉ
|
| 56 |
)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
| 1 |
gradio==5.35.0
|
| 2 |
-
transformers
|
| 3 |
-
torch
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio==5.35.0
|
| 2 |
+
transformers==4.35.0
|
| 3 |
+
torch==2.1.0
|
| 4 |
+
Pillow==10.0.0
|
| 5 |
+
requests==2.31.0
|
| 6 |
+
numpy==1.24.3
|