Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,24 +16,40 @@ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %
|
|
| 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
|
| 20 |
-
"""์ด๋ฏธ์ง ์์ฑ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
client = Client(IMAGE_API_URL)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
)
|
| 38 |
return result[0], result[1]
|
| 39 |
except Exception as e:
|
|
@@ -49,6 +65,7 @@ def respond(
|
|
| 49 |
top_p=0.9,
|
| 50 |
):
|
| 51 |
system_prefix = """
|
|
|
|
| 52 |
[์์คํ
ํ๋กฌํํธ ๋ด์ฉ...]
|
| 53 |
"""
|
| 54 |
|
|
@@ -62,7 +79,6 @@ def respond(
|
|
| 62 |
|
| 63 |
response = ""
|
| 64 |
try:
|
| 65 |
-
# ํ
์คํธ ์์ฑ
|
| 66 |
for msg in hf_client.chat_completion(
|
| 67 |
messages,
|
| 68 |
max_tokens=max_tokens,
|
|
@@ -73,23 +89,27 @@ def respond(
|
|
| 73 |
token = msg.choices[0].delta.content
|
| 74 |
if token is not None:
|
| 75 |
response += token.strip("")
|
| 76 |
-
# ์ฑํ
ํ์คํ ๋ฆฌ์ ๋ง๋ ํ์์ผ๋ก ๋ฐํ
|
| 77 |
history = history + [(message, response)]
|
| 78 |
-
yield history
|
| 79 |
-
|
| 80 |
-
# ํ
์คํธ ์์ฑ์ด ์๋ฃ๋ ํ ์ด๋ฏธ์ง ์์ฑ
|
| 81 |
-
try:
|
| 82 |
-
image, seed = generate_image(response[:200])
|
| 83 |
-
history = history[:-1] + [(message, response)] # ๋ง์ง๋ง ๋ฉ์์ง ์
๋ฐ์ดํธ
|
| 84 |
-
yield history, image
|
| 85 |
-
except Exception as e:
|
| 86 |
-
logging.error(f"Image generation failed: {str(e)}")
|
| 87 |
-
yield history, None
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
error_message = f"Error: {str(e)}"
|
| 91 |
history = history + [(message, error_message)]
|
| 92 |
-
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 95 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as interface:
|
|
@@ -103,11 +123,14 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as interface:
|
|
| 103 |
label="Chat History",
|
| 104 |
height=500
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
system_msg = gr.Textbox(
|
| 112 |
label="System Message",
|
| 113 |
value="Write(output) in ํ๊ตญ์ด.",
|
|
@@ -156,10 +179,16 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as interface:
|
|
| 156 |
)
|
| 157 |
|
| 158 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
msg.submit(
|
| 160 |
-
|
| 161 |
-
[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 162 |
-
[chatbot, image_output]
|
| 163 |
)
|
| 164 |
|
| 165 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
|
|
|
| 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 |
+
async def generate_image_prompt(text: str) -> str:
|
| 20 |
+
"""์
๋ ฅ ํ
์คํธ๋ก๋ถํฐ ์ด๋ฏธ์ง ์์ฑ์ฉ ํ๋กฌํํธ ์์ฑ"""
|
| 21 |
+
try:
|
| 22 |
+
prompt_messages = [
|
| 23 |
+
{"role": "system", "content": "You are an expert at creating image generation prompts. Convert the input text into a detailed image generation prompt that describes the visual elements, style, and atmosphere."},
|
| 24 |
+
{"role": "user", "content": f"Create an image generation prompt for: {text}"}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
response = hf_client.chat_completion(prompt_messages, max_tokens=200)
|
| 28 |
+
image_prompt = response.choices[0].message.content
|
| 29 |
+
return f"fantasy style, {image_prompt}"
|
| 30 |
+
except Exception as e:
|
| 31 |
+
logging.error(f"Image prompt generation failed: {str(e)}")
|
| 32 |
+
return f"fantasy style, {text}"
|
| 33 |
+
|
| 34 |
+
async def generate_image_async(prompt: str) -> tuple:
|
| 35 |
+
"""๋น๋๊ธฐ ์ด๋ฏธ์ง ์์ฑ ํจ์"""
|
| 36 |
try:
|
| 37 |
client = Client(IMAGE_API_URL)
|
| 38 |
+
result = await asyncio.get_event_loop().run_in_executor(
|
| 39 |
+
ThreadPoolExecutor(),
|
| 40 |
+
lambda: client.predict(
|
| 41 |
+
prompt=prompt,
|
| 42 |
+
width=768,
|
| 43 |
+
height=768,
|
| 44 |
+
guidance=7.5,
|
| 45 |
+
inference_steps=30,
|
| 46 |
+
seed=3,
|
| 47 |
+
do_img2img=False,
|
| 48 |
+
init_image=None,
|
| 49 |
+
image2image_strength=0.8,
|
| 50 |
+
resize_img=True,
|
| 51 |
+
api_name="/generate_image"
|
| 52 |
+
)
|
| 53 |
)
|
| 54 |
return result[0], result[1]
|
| 55 |
except Exception as e:
|
|
|
|
| 65 |
top_p=0.9,
|
| 66 |
):
|
| 67 |
system_prefix = """
|
| 68 |
+
You are 'FantasyAIโจ', an advanced AI storyteller specialized in creating immersive fantasy narratives.
|
| 69 |
[์์คํ
ํ๋กฌํํธ ๋ด์ฉ...]
|
| 70 |
"""
|
| 71 |
|
|
|
|
| 79 |
|
| 80 |
response = ""
|
| 81 |
try:
|
|
|
|
| 82 |
for msg in hf_client.chat_completion(
|
| 83 |
messages,
|
| 84 |
max_tokens=max_tokens,
|
|
|
|
| 89 |
token = msg.choices[0].delta.content
|
| 90 |
if token is not None:
|
| 91 |
response += token.strip("")
|
|
|
|
| 92 |
history = history + [(message, response)]
|
| 93 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
error_message = f"Error: {str(e)}"
|
| 97 |
history = history + [(message, error_message)]
|
| 98 |
+
yield history
|
| 99 |
+
|
| 100 |
+
async def process_input(message, history, system_message, max_tokens, temperature, top_p):
|
| 101 |
+
"""์
๋ ฅ ์ฒ๋ฆฌ ๋ฐ ์ด๋ฏธ์ง ์์ฑ ํตํฉ ํจ์"""
|
| 102 |
+
# ์ด๋ฏธ์ง ํ๋กฌํํธ ์์ฑ ๋ฐ ์ด๋ฏธ์ง ์์ฑ ์์
|
| 103 |
+
image_prompt = await generate_image_prompt(message)
|
| 104 |
+
image_future = asyncio.create_task(generate_image_async(image_prompt))
|
| 105 |
+
|
| 106 |
+
# ํ
์คํธ ์๋ต ์์ฑ
|
| 107 |
+
text_response = respond(message, history, system_message, max_tokens, temperature, top_p)
|
| 108 |
+
|
| 109 |
+
# ์ด๋ฏธ์ง ์์ฑ ์๋ฃ ๋๊ธฐ
|
| 110 |
+
image, seed = await image_future
|
| 111 |
+
|
| 112 |
+
return text_response, image
|
| 113 |
|
| 114 |
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 115 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as interface:
|
|
|
|
| 123 |
label="Chat History",
|
| 124 |
height=500
|
| 125 |
)
|
| 126 |
+
with gr.Row():
|
| 127 |
+
msg = gr.Textbox(
|
| 128 |
+
label="Enter your message",
|
| 129 |
+
placeholder="Type your message here...",
|
| 130 |
+
lines=2
|
| 131 |
+
)
|
| 132 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 133 |
+
|
| 134 |
system_msg = gr.Textbox(
|
| 135 |
label="System Message",
|
| 136 |
value="Write(output) in ํ๊ตญ์ด.",
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 182 |
+
submit_btn.click(
|
| 183 |
+
fn=process_input,
|
| 184 |
+
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 185 |
+
outputs=[chatbot, image_output]
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
msg.submit(
|
| 189 |
+
fn=process_input,
|
| 190 |
+
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 191 |
+
outputs=[chatbot, image_output]
|
| 192 |
)
|
| 193 |
|
| 194 |
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|