Manggomee commited on
Commit
460508b
ยท
verified ยท
1 Parent(s): 1859be9

Upload 4 files

Browse files
Files changed (1) hide show
  1. app.py +71 -45
app.py CHANGED
@@ -1,56 +1,82 @@
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
- )
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import random
4
+ from PIL import Image, ImageDraw, ImageFont
5
+ import io
6
 
7
+ # ๋ชจ๋ธ ์ดˆ๊ธฐํ™”
8
+ try:
9
+ qa_pipeline = pipeline("question-answering")
10
+ except:
11
+ qa_pipeline = None
12
 
13
+ def create_meme(template_choice, top_text, bottom_text):
14
+ """๋ฐˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ"""
15
  try:
16
+ # ๊ฐ„๋‹จํ•œ ๋ฐˆ ์ด๋ฏธ์ง€ ์ƒ์„ฑ
17
+ width, height = 500, 400
18
+ img = Image.new('RGB', (width, height), color='lightblue')
19
+ draw = ImageDraw.Draw(img)
20
+
21
+ # ํ…์ŠคํŠธ ์ถ”๊ฐ€
22
+ if top_text:
23
+ draw.text((50, 50), top_text, fill='black')
24
+ if bottom_text:
25
+ draw.text((50, 350), bottom_text, fill='black')
26
+
27
+ return img
28
  except Exception as e:
29
+ return f"์˜ค๋ฅ˜: {str(e)}"
30
+
31
+ def chatbot_response(message, history):
32
+ """์ฑ—๋ด‡ ์‘๋‹ต"""
33
+ responses = [
34
+ "ํฅ๋ฏธ๋กœ์šด ์งˆ๋ฌธ์ด๋„ค์š”!",
35
+ "๋” ์ž์„ธํžˆ ๋ง์”€ํ•ด์ฃผ์„ธ์š”.",
36
+ "๊ทธ๊ฒƒ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.",
37
+ "์ข‹์€ ์•„์ด๋””์–ด์ž…๋‹ˆ๋‹ค!"
38
+ ]
39
+ return random.choice(responses)
40
 
41
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค
42
+ with gr.Blocks(title="๋ฐˆ ์ƒ์„ฑ & ์ฑ—๋ด‡") as demo:
43
+ gr.Markdown("# ๐ŸŽญ ๋ฐˆ ์ƒ์„ฑ & AI ์ฑ—๋ด‡ ์„œ๋น„์Šค")
44
 
45
+ with gr.Tabs():
46
+ # ๋ฐˆ ์ƒ์„ฑ ํƒญ
47
+ with gr.TabItem("๐Ÿ–ผ๏ธ ๋ฐˆ ์ƒ์„ฑ๊ธฐ"):
48
+ with gr.Row():
49
+ with gr.Column():
50
+ template_dropdown = gr.Dropdown(
51
+ choices=["๋“œ๋ ˆ์ดํฌ ๋ฐˆ", "๋””์ŠคํŠธ๋ž™ํ‹ฐ๋“œ ๋ณด์ดํ”„๋ Œ๋“œ", "์› ๋‘์Šค ๋‚ซ ์‹ฌํ”Œ๋ฆฌ"],
52
+ label="๋ฐˆ ํ…œํ”Œ๋ฆฟ ์„ ํƒ",
53
+ value="๋“œ๋ ˆ์ดํฌ ๋ฐˆ"
54
+ )
55
+ top_text = gr.Textbox(label="์ƒ๋‹จ ํ…์ŠคํŠธ", value="When you see a bug")
56
+ bottom_text = gr.Textbox(label="ํ•˜๋‹จ ํ…์ŠคํŠธ", value="But it works in production")
57
+ create_btn = gr.Button("๋ฐˆ ์ƒ์„ฑํ•˜๊ธฐ", variant="primary")
58
+
59
+ with gr.Column():
60
+ meme_output = gr.Image(label="์ƒ์„ฑ๋œ ๋ฐˆ")
61
+
62
+ create_btn.click(create_meme, [template_dropdown, top_text, bottom_text], meme_output)
63
 
64
+ # ์ฑ—๋ด‡ ํƒญ
65
+ with gr.TabItem("๐Ÿค– AI ์ฑ—๋ด‡"):
66
+ chatbot = gr.Chatbot(label="AI ์–ด์‹œ์Šคํ„ดํŠธ", height=400)
67
+ msg = gr.Textbox(label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ", placeholder="๊ถ๊ธˆํ•œ ๊ฒƒ์„ ๋ฌผ์–ด๋ณด์„ธ์š”...")
68
+
69
+ def user_msg(message, history):
70
+ return "", history + [[message, None]]
71
+
72
+ def bot_msg(history):
73
+ if history and history[-1][1] is None:
74
+ user_message = history[-1][0]
75
+ bot_response = chatbot_response(user_message, history)
76
+ history[-1][1] = bot_response
77
+ return history
78
+
79
+ msg.submit(user_msg, [msg, chatbot], [msg, chatbot]).then(bot_msg, chatbot, chatbot)
80
 
 
81
  if __name__ == "__main__":
82
+ demo.launch()