Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,43 @@
|
|
2 |
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
|
|
5 |
import os
|
6 |
import requests
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
|
|
|
|
|
|
9 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
def respond(
|
12 |
message,
|
@@ -40,38 +72,57 @@ def respond(
|
|
40 |
token = message.choices[0].delta.content
|
41 |
if token is not None:
|
42 |
response += token.strip("")
|
43 |
-
yield response
|
|
|
|
|
|
|
|
|
|
|
44 |
except Exception as e:
|
45 |
-
yield f"Error: {str(e)}"
|
46 |
|
47 |
# Gradio ์ธํฐํ์ด์ค ์ค์
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
gr.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
72 |
if __name__ == "__main__":
|
73 |
interface.launch(
|
74 |
-
server_name="0.0.0.0",
|
75 |
-
server_port=7860,
|
76 |
-
share=True
|
77 |
-
)
|
|
|
2 |
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
+
from gradio_client import Client
|
6 |
import os
|
7 |
import requests
|
8 |
+
import asyncio
|
9 |
+
import logging
|
10 |
+
from concurrent.futures import ThreadPoolExecutor
|
11 |
|
12 |
+
# ๋ก๊น
์ค์
|
13 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
+
|
15 |
+
# API ์ค์
|
16 |
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
17 |
+
IMAGE_API_URL = "http://211.233.58.201:7896"
|
18 |
+
|
19 |
+
def generate_image(prompt: str) -> tuple:
|
20 |
+
"""์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
21 |
+
try:
|
22 |
+
client = Client(IMAGE_API_URL)
|
23 |
+
# ํ๋กฌํํธ ์์ "fantasy style," ์ถ๊ฐ
|
24 |
+
enhanced_prompt = f"fantasy style, {prompt}"
|
25 |
+
result = client.predict(
|
26 |
+
prompt=enhanced_prompt,
|
27 |
+
width=768,
|
28 |
+
height=768,
|
29 |
+
guidance=7.5,
|
30 |
+
inference_steps=30,
|
31 |
+
seed=3,
|
32 |
+
do_img2img=False,
|
33 |
+
init_image=None,
|
34 |
+
image2image_strength=0.8,
|
35 |
+
resize_img=True,
|
36 |
+
api_name="/generate_image"
|
37 |
+
)
|
38 |
+
return result[0], result[1]
|
39 |
+
except Exception as e:
|
40 |
+
logging.error(f"Image generation failed: {str(e)}")
|
41 |
+
return None, f"Error: {str(e)}"
|
42 |
|
43 |
def respond(
|
44 |
message,
|
|
|
72 |
token = message.choices[0].delta.content
|
73 |
if token is not None:
|
74 |
response += token.strip("")
|
75 |
+
yield response, None # ์ด๋ฏธ์ง๋ฅผ ์ํ None ์ถ๊ฐ
|
76 |
+
|
77 |
+
# ํ
์คํธ ์์ฑ์ด ์๋ฃ๋ ํ ์ด๋ฏธ์ง ์์ฑ
|
78 |
+
image, seed = generate_image(response[:200]) # ์ฒ์ 200์๋ฅผ ์ด๋ฏธ์ง ํ๋กฌํํธ๋ก ์ฌ์ฉ
|
79 |
+
yield response, image
|
80 |
+
|
81 |
except Exception as e:
|
82 |
+
yield f"Error: {str(e)}", None
|
83 |
|
84 |
# Gradio ์ธํฐํ์ด์ค ์ค์
|
85 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as interface:
|
86 |
+
gr.Markdown("# Fantasy Novel AI Generation")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column(scale=2):
|
90 |
+
chatbot = gr.Chatbot()
|
91 |
+
msg = gr.Textbox(label="Enter your message")
|
92 |
+
system_msg = gr.Textbox(label="System Message", value="Write(output) in ํ๊ตญ์ด.")
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
max_tokens = gr.Slider(minimum=1, maximum=8000, value=7000, label="Max Tokens")
|
96 |
+
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
|
97 |
+
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
98 |
+
|
99 |
+
with gr.Column(scale=1):
|
100 |
+
image_output = gr.Image(label="Generated Image")
|
101 |
+
|
102 |
+
examples = gr.Examples(
|
103 |
+
examples=[
|
104 |
+
["ํํ์ง ์์ค์ ํฅ๋ฏธ๋ก์ด ์์ฌ 10๊ฐ์ง๋ฅผ ์ ์ํ๋ผ"],
|
105 |
+
["๊ณ์ ์ด์ด์ ์์ฑํ๋ผ"],
|
106 |
+
["Translate into English"],
|
107 |
+
["๋ง๋ฒ ์์คํ
์ ๋ํด ๋ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
108 |
+
["์ ํฌ ์ฅ๋ฉด์ ๋ ๊ทน์ ์ผ๋ก ๋ฌ์ฌํ๋ผ"],
|
109 |
+
["์๋ก์ด ํํ์ง ์ข
์กฑ์ ์ถ๊ฐํ๋ผ"],
|
110 |
+
["๊ณ ๋ ์์ธ์ ๋ํด ๋ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
111 |
+
["์ฃผ์ธ๊ณต์ ๋ด๋ฉด ๋ฌ์ฌ๋ฅผ ์ถ๊ฐํ๋ผ"],
|
112 |
+
],
|
113 |
+
inputs=msg
|
114 |
+
)
|
115 |
+
|
116 |
+
msg.submit(
|
117 |
+
respond,
|
118 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
119 |
+
[chatbot, image_output]
|
120 |
+
)
|
121 |
|
122 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
123 |
if __name__ == "__main__":
|
124 |
interface.launch(
|
125 |
+
server_name="0.0.0.0",
|
126 |
+
server_port=7860,
|
127 |
+
share=True
|
128 |
+
)
|