SabaGul commited on
Commit
8996529
·
verified ·
1 Parent(s): 8d4b80b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from PIL import Image
4
+ from transformers import pipeline
5
+ from langchain_openai import OpenAI
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain_openai import ChatOpenAI
8
+ from langchain.chains import LLMChain
9
+ import os
10
+
11
+ # Set your OpenAI API key here
12
+ OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
13
+
14
+ # Initialize the image captioning model
15
+ captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
16
+
17
+ # Initialize the OpenAI model
18
+ #llm = OpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=512, openai_api_key=OPENAI_API_KEY)
19
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, max_tokens=512, openai_api_key=OPENAI_API_KEY)
20
+ template = """Question: {question}
21
+ Answer: Lets think step by step,"""
22
+ prompt = PromptTemplate(template=template, input_variables=["question"])
23
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
24
+
25
+ def get_recipe(image):
26
+ # Use the captioning model to generate a description of the image
27
+ image = Image.open(image)
28
+ results = captioner(image)
29
+ description = results[0]['generated_text']
30
+
31
+ # Use the description to get a recipe
32
+ question = f"give me the list of ingredients and a step by step recipe for the food in {description}"
33
+ openai_results = llm_chain.invoke(question)
34
+
35
+ return str(openai_results['text'])
36
+
37
+ # Define the Gradio interface
38
+ iface = gr.Interface(
39
+ fn=get_recipe,
40
+ inputs=gr.Image(type="filepath"),
41
+ outputs=gr.Text(),
42
+ title="Recipe Generator",
43
+ description="Upload an image of food to get a recipe."
44
+ )
45
+
46
+ # Launch the app
47
+ iface.launch(debug=True)