import os import gradio as gr from huggingface_hub import InferenceClient class HaikuGenerator: def __init__(self): HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN") self.text_client = InferenceClient(token=HUGGINGFACE_API_TOKEN, model="HuggingFaceH4/zephyr-7b-beta") self.image_client = InferenceClient(token=HUGGINGFACE_API_TOKEN, model="stabilityai/stable-diffusion-xl-base-1.0") def generate_haiku(self, prompt): system_message = "You are a Haiku generator." response = "" messages = [{"role": "system", "content": system_message}, {"role": "user", "content": prompt}] # Get the chat completion response message = self.text_client.chat_completion( messages, max_tokens=30, stream=False, temperature=0.7, top_p=0.95, ) # Extract the generated content response = message['choices'][0]['message']['content'] return response.strip() def text_to_image(self, prompt, style): # Modify the prompt based on the selected style if style == "Japanese": prompt += ", in Japanese art style" elif style == "oil painting": prompt += ", in the style of an oil painting" image = self.image_client.text_to_image(prompt) return image def gradio_interface(self): # Custom CSS to apply a Japanese-style font custom_css = """ body { font-family: 'Sawarabi Mincho', serif !important; } h1, h6 { font-family: 'Sawarabi Mincho', serif !important; } .button { font-family: 'Sawarabi Mincho', serif !important; } """ with gr.Blocks(theme='earneleh/paris', css=custom_css) as demo: gr.HTML("""