File size: 1,611 Bytes
8996529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)