import gradio as gr import os import requests import json SYSTEM_PROMPT = "As an LLM, your job is to generate detailed prompts that start with generate the image, for image generation models based on user input. Be descriptive and specific, but also make sure your prompts are clear and concise." TITLE = "Image Prompter" EXAMPLE_INPUTS = [ {"prompt": "A Reflective cat between stars.", "image_url": "https://www.bing.com/images/create/a-black-cat-with-a-shiny2c-reflective-coat-is-float/1-656c50e048424f578a489a4875acd14f?id=%2b0DNSc2C8Sw26e32dIzHZA%3d%3d&view=detailv2&idpp=genimg&idpclose=1&FORM=SYDBIC"}, {"prompt": "A Stunning sunset over the mountains.", "image_url": "https://www.example.com/sunset_image.jpg"}, {"prompt": "An Enchanted forest with fireflies.", "image_url": "https://www.example.com/forest_image.jpg"}, {"prompt": "A Mysterious spaceship in the night sky.", "image_url": "https://www.example.com/spaceship_image.jpg"} ] html_temp = """
Image 1

{prompt_1}

Image 2

{prompt_2}

Image 3

{prompt_3}

Image 4

{prompt_4}

""" zephyr_7b_beta = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta/" HF_TOKEN = os.getenv("HF_TOKEN") HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"} def build_input_prompt(message, chatbot, system_prompt): input_prompt = "\n" + system_prompt + "\n\n" for interaction in chatbot: input_prompt = input_prompt + str(interaction[0]) + "\n\n" + str(interaction[1]) + "\n\n\n" input_prompt = input_prompt + str(message) + "\n" return input_prompt def post_request_beta(payload): response = requests.post(zephyr_7b_beta, headers=HEADERS, json=payload) response.raise_for_status() return response.json() def predict_beta(message, chatbot=[], system_prompt=""): input_prompt = build_input_prompt(message, chatbot, system_prompt) data = {"inputs": input_prompt} try: response_data = post_request_beta(data) json_obj = response_data[0] if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0: bot_message = json_obj['generated_text'] return bot_message elif 'error' in json_obj: raise gr.Error(json_obj['error'] + ' Please refresh and try again with smaller input prompt') else: warning_msg = f"Unexpected response: {json_obj}" raise gr.Error(warning_msg) except requests.HTTPError as e: error_msg = f"Request failed with status code {e.response.status_code}" raise gr.Error(error_msg) except json.JSONDecodeError as e: error_msg = f"Failed to decode response as JSON: {str(e)}" raise gr.Error(error_msg) def test_preview_chatbot(message, history): response = predict_beta(message, history, SYSTEM_PROMPT) return response # Display HTML and launch the interface gr.Interface( fn=test_preview_chatbot, live=False, examples=[[EXAMPLE_INPUTS[0]['prompt']]], inputs=gr.Textbox(scale=7, container=False, value=EXAMPLE_INPUTS[0]['prompt']), outputs=gr.Textbox(), layout="vertical", html=html_temp.format( image_url_1=EXAMPLE_INPUTS[0]["image_url"], prompt_1=EXAMPLE_INPUTS[0]["prompt"], image_url_2=EXAMPLE_INPUTS[1]["image_url"], prompt_2=EXAMPLE_INPUTS[1]["prompt"], image_url_3=EXAMPLE_INPUTS[2]["image_url"], prompt_3=EXAMPLE_INPUTS[2]["prompt"], image_url_4=EXAMPLE_INPUTS[3]["image_url"], prompt_4=EXAMPLE_INPUTS[3]["prompt"], ), ).launch(share=True)