Manggomee commited on
Commit
4064100
ยท
verified ยท
1 Parent(s): d0a7e74

Upload 4 files

Browse files
Files changed (2) hide show
  1. app.py +38 -140
  2. 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
- # AI ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
12
- try:
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 create_meme(template_choice, top_text, bottom_text):
21
- """๋ฐˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ•จ์ˆ˜"""
22
  try:
23
- if template_choice in MEME_TEMPLATES:
24
- template_info = MEME_TEMPLATES[template_choice]
25
- # ๊ธฐ๋ณธ ํ…œํ”Œ๋ฆฟ ์ด๋ฏธ์ง€ ์ƒ์„ฑ (์‹ค์ œ๋กœ๋Š” ๋ฏธ๋ฆฌ ์ค€๋น„๋œ ์ด๋ฏธ์ง€ ์‚ฌ์šฉ)
26
- img = create_meme_image(template_info, top_text, bottom_text)
27
- return img
28
- else:
29
- return "ํ…œํ”Œ๋ฆฟ์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”."
30
  except Exception as e:
31
- return f"๋ฐˆ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
32
 
33
- def chatbot_response(message, history):
34
- """์ฑ—๋ด‡ ์‘๋‹ต ์ƒ์„ฑ"""
35
- try:
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.Tabs():
61
- # ๋ฐˆ ์ƒ์„ฑ ํƒญ
62
- with gr.TabItem("๐Ÿ–ผ๏ธ ๋ฐˆ ์ƒ์„ฑ๊ธฐ"):
63
- with gr.Row():
64
- with gr.Column():
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
- with gr.Row():
105
- send_btn = gr.Button("์ „์†ก", variant="primary")
106
- clear_btn = gr.Button("๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
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
- send_btn.click(user_message, [msg_input, chatbot], [msg_input, chatbot]).then(
122
- bot_message, chatbot, chatbot
123
- )
124
- clear_btn.click(lambda: [], outputs=chatbot)
125
 
126
- # ํŠธ๋ Œ๋“œ ๋ถ„์„ ํƒญ
127
- with gr.TabItem("๐Ÿ“ˆ ํŠธ๋ Œ๋“œ ๋ถ„์„"):
128
- with gr.Row():
129
- with gr.Column():
130
- keyword_input = gr.Textbox(
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
- gr.Markdown("---")
151
- gr.Markdown("๐Ÿ’ก **์‚ฌ์šฉ๋ฒ•**: ๊ฐ ํƒญ์„ ํด๋ฆญํ•˜์—ฌ ๋ฐˆ ์ƒ์„ฑ, AI ์ฑ„ํŒ…, ํŠธ๋ Œ๋“œ ๋ถ„์„ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜์„ธ์š”!")
 
 
 
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