import gradio as gr import requests from PIL import Image from transformers import pipeline from langchain_openai import OpenAI from langchain.prompts import PromptTemplate from langchain_openai import ChatOpenAI from langchain.chains import LLMChain import os # Set your OpenAI API key here OPENAI_API_KEY=os.getenv('OPENAI_API_KEY') # Initialize the image captioning model captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") # Initialize the OpenAI model #llm = OpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=512, openai_api_key=OPENAI_API_KEY) llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, max_tokens=512, openai_api_key=OPENAI_API_KEY) template = """Question: {question} Answer: Lets think step by step,""" prompt = PromptTemplate(template=template, input_variables=["question"]) llm_chain = LLMChain(prompt=prompt, llm=llm) def get_recipe(image): # Use the captioning model to generate a description of the image image = Image.open(image) results = captioner(image) description = results[0]['generated_text'] # Use the description to get a recipe question = f"give me the list of ingredients and a step by step recipe for the food in {description}" openai_results = llm_chain.invoke(question) return str(openai_results['text']) # Define the Gradio interface iface = gr.Interface( fn=get_recipe, inputs=gr.Image(type="filepath"), outputs=gr.Text(), title="Recipe Generator", description="Upload an image of food to get a recipe." ) # Launch the app iface.launch(debug=True)