|
1 | 1 | import os
|
2 | 2 | from dotenv import load_dotenv
|
3 |
| -from langchain import PromptTemplate, LLMChain |
| 3 | +from langchain_core.prompts import PromptTemplate |
4 | 4 | from transformers import pipeline
|
5 | 5 | import streamlit as st
|
6 |
| -from langchain.llms.bedrock import Bedrock |
| 6 | +import boto3 |
| 7 | +from langchain_aws import ChatBedrock |
7 | 8 |
|
8 | 9 | PAGE_CONFIG = {"page_title":"Image to Recipe", "page_icon":":chef:", "layout":"centered"}
|
9 | 10 | st.set_page_config(**PAGE_CONFIG)
|
|
24 | 25 | """, unsafe_allow_html=True)
|
25 | 26 |
|
26 | 27 | def get_llm():
|
27 |
| - bedrock_llm = Bedrock(model_id="anthropic.claude-v2", |
28 |
| - model_kwargs={"temperature": 0.7, "max_tokens_to_sample": 4096}) |
| 28 | + |
| 29 | + bedrock_client = boto3.client('bedrock-runtime') |
| 30 | + bedrock_llm = ChatBedrock( |
| 31 | + provider="anthropic", |
| 32 | + model_id="anthropic.claude-3-5-sonnet-20240620-v1:0", |
| 33 | + client = bedrock_client, |
| 34 | + model_kwargs={"max_tokens": 4096, "temperature": 0.7}, |
| 35 | + ) |
29 | 36 | return bedrock_llm
|
30 | 37 |
|
31 | 38 | def image_to_text(url):
|
@@ -87,12 +94,12 @@ def generate_recipe(ingredients):
|
87 | 94 | """
|
88 | 95 |
|
89 | 96 | with st.spinner('Making the recipe for you...'):
|
90 |
| - prompt = PromptTemplate(template=template, input_variables=["ingredients"]) |
| 97 | + prompt = PromptTemplate.from_template(template=template) |
| 98 | + prompt_formatted_str: str = prompt.format(ingredients=ingredients) |
91 | 99 | llm = get_llm()
|
92 |
| - recipe_chain = LLMChain(llm=llm, prompt=prompt, verbose=True) |
93 |
| - recipe = recipe_chain.run(ingredients) |
| 100 | + recipe = llm.invoke(input=prompt_formatted_str) |
94 | 101 |
|
95 |
| - return recipe |
| 102 | + return recipe.content |
96 | 103 |
|
97 | 104 | def main():
|
98 | 105 |
|
@@ -151,8 +158,5 @@ def main():
|
151 | 158 | os.remove(upload_file.name)
|
152 | 159 |
|
153 | 160 | if __name__ == "__main__":
|
154 |
| - load_dotenv() |
155 |
| - OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
156 |
| - HUGGINFACE_HUB_API_TOKEN = os.getenv("HUGGINFACE_HUB_API_TOKEN") |
157 |
| - |
| 161 | + load_dotenv() |
158 | 162 | main()
|
0 commit comments