File size: 1,501 Bytes
b026171
 
 
 
 
df41970
b026171
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# utils.py
import streamlit as st
import base64
import config

# --- for prompt injection detection ---


def contains_injection_keywords(text):
    keywords = ["ignore previous", "ignore instructions", "disregard",
                "forget your instructions", "act as", "you must", "system prompt:"]
    lower_text = text.lower()
    return any(keyword in lower_text for keyword in keywords)


# --- 画像生成関数 ---
def generate_image_from_prompt(_together_client, prompt_text):
    """Generates an image using Together AI and returns image bytes."""
    try:
        response = _together_client.images.generate(
            prompt=prompt_text,
            model=config.IMAGE_MODEL,
            width=config.IMAGE_WIDTH,
            height=config.IMAGE_HEIGHT,
            steps=config.IMAGE_STEPS,
            n=1,
            response_format=config.IMAGE_RESPONSE_FORMAT,
            # stop=[] # stopは通常不要
        )
        if response.data and response.data[0].b64_json:
            b64_data = response.data[0].b64_json
            image_bytes = base64.b64decode(b64_data)
            return image_bytes
        else:
            st.error("Image generation failed: No image data received.")
            return None
    except Exception as e:
        st.error(f"Image generation error: {e}", icon="🚨")
        return None


def display_icon(emoji: str):
    st.write(
        f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
        unsafe_allow_html=True,
    )