import gradio as gr import os, sys from prompting import promptingutils from imageprocessing import imageprocessingtools from openai import OpenAI from prompting.promptingutils import DEFAULT_N_SAMPLES, DEFAULT_OBJECT_THRESHOLD, DEFAULT_RANDOM_STATE AVAILABLE_LLMS = [ "mistral-7b-instruct", "vicuna-7b", "llama-7b-chat", "vicuna-13b", ] DEFAULT_TEMPERATURE = 0 LLAMA_API_TOKEN = os.environ["LLAMA_API_TOKEN"] client = OpenAI( api_key = LLAMA_API_TOKEN, base_url = "https://api.llama-api.com" ) def caption_artwork( image_filepath: os.PathLike, llm :str, temperature = DEFAULT_TEMPERATURE, items_threshold = DEFAULT_OBJECT_THRESHOLD, random_state = DEFAULT_RANDOM_STATE, n_samples_per_emotion = DEFAULT_N_SAMPLES )-> tuple: all_information = imageprocessingtools.extract_all_information_from_image(image_filepath) emotion = all_information["emotion"] colors_list = all_information["colors_list"] objects_and_probs = all_information["objects_and_probs"] objects_list = promptingutils.filter_items(objects_and_probs, items_threshold=items_threshold) user_prompt = promptingutils.get_user_prompt( colors_list=colors_list, objects_list=objects_list, emotion=emotion, n_samples_per_emotion=n_samples_per_emotion, random_state=random_state, object_threshold=items_threshold ) response = client.chat.completions.create( model = llm, messages = [ {"role": "system" , "content": "Assistant is a large language model trained by OpenAI."}, {"role": "user" , "content": user_prompt} ], temperature = temperature ) commentary_str = response.choices[0].message.content colors_str = ", ".join(colors_list) objects_str = ", ".join(objects_list) emotion_str = emotion return (emotion_str, colors_str, objects_str, commentary_str) with gr.Blocks() as demo: with gr.Column(): gr.HTML("""