Spaces:
Runtime error
Runtime error
Commit
·
ef34ec3
1
Parent(s):
a4febd7
Initial commit
Browse files- app.py +114 -0
- env.example +3 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
from tempfile import NamedTemporaryFile
|
5 |
+
|
6 |
+
import replicate
|
7 |
+
import streamlit as st
|
8 |
+
from PIL import Image
|
9 |
+
from langchain.callbacks.base import BaseCallbackHandler
|
10 |
+
from langchain.chat_models import ChatOpenAI
|
11 |
+
from langchain.schema import ChatMessage
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
MODEL_NAME = os.getenv("MODEL_NAME")
|
17 |
+
os.environ["REPLICATE_API_TOKEN"] = os.getenv("REPLICATE_API_TOKEN")
|
18 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
19 |
+
|
20 |
+
SYSTEM_PROMPT = (
|
21 |
+
"You're a chef and an helpful assistant who suggests delicious recipes that includes foods which are "
|
22 |
+
"nutritionally beneficial, easy & not time consuming, healthy yet economical."
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
+
class StreamHandler(BaseCallbackHandler):
|
27 |
+
def __init__(self, container, initial_text=""):
|
28 |
+
self.container = container
|
29 |
+
self.text = initial_text
|
30 |
+
|
31 |
+
def on_llm_new_token(self, token: str, **kwargs) -> None:
|
32 |
+
self.text += token
|
33 |
+
self.container.markdown(self.text)
|
34 |
+
|
35 |
+
|
36 |
+
st.title('Chef:green[GPT] :cook:')
|
37 |
+
|
38 |
+
|
39 |
+
with st.sidebar:
|
40 |
+
st.title("Add grocery items")
|
41 |
+
|
42 |
+
uploaded_files = st.file_uploader(
|
43 |
+
label="Upload image of each item",
|
44 |
+
key="files_upload",
|
45 |
+
accept_multiple_files=True,
|
46 |
+
)
|
47 |
+
|
48 |
+
cook = st.button("Let AI cook!")
|
49 |
+
|
50 |
+
if cook:
|
51 |
+
for uploaded_file in uploaded_files:
|
52 |
+
bytes_data = uploaded_file.getvalue()
|
53 |
+
image = Image.open(io.BytesIO(bytes_data))
|
54 |
+
st.image(image, width=100)
|
55 |
+
|
56 |
+
with NamedTemporaryFile(suffix=".jpg") as temp_file:
|
57 |
+
image.save(temp_file.name)
|
58 |
+
output = replicate.run(
|
59 |
+
MODEL_NAME,
|
60 |
+
input={"image": open(temp_file.name, "rb")},
|
61 |
+
)
|
62 |
+
pattern = r"Caption:\s(.*)"
|
63 |
+
match = re.search(pattern, output).group(1)
|
64 |
+
|
65 |
+
if "groceries" not in st.session_state:
|
66 |
+
st.session_state["groceries"] = ""
|
67 |
+
st.session_state["groceries"] += f"{match},"
|
68 |
+
|
69 |
+
|
70 |
+
if "messages" not in st.session_state:
|
71 |
+
st.session_state["messages"] = [
|
72 |
+
ChatMessage(role="system", content=SYSTEM_PROMPT),
|
73 |
+
ChatMessage(role="assistant", content="How can I help you?"),
|
74 |
+
]
|
75 |
+
|
76 |
+
for msg in st.session_state.messages:
|
77 |
+
st.chat_message(msg.role).write(msg.content)
|
78 |
+
|
79 |
+
if prompt := st.chat_input():
|
80 |
+
st.session_state.messages.append(ChatMessage(role="user", content=prompt))
|
81 |
+
st.chat_message("user").write(prompt)
|
82 |
+
|
83 |
+
if not openai_api_key:
|
84 |
+
st.info("Please add your OpenAI API key to continue.")
|
85 |
+
st.stop()
|
86 |
+
|
87 |
+
with st.chat_message("assistant"):
|
88 |
+
stream_handler = StreamHandler(st.empty())
|
89 |
+
llm = ChatOpenAI(
|
90 |
+
openai_api_key=openai_api_key, streaming=True, callbacks=[stream_handler]
|
91 |
+
)
|
92 |
+
response = llm(st.session_state.messages)
|
93 |
+
st.session_state.messages.append(
|
94 |
+
ChatMessage(role="assistant", content=response.content)
|
95 |
+
)
|
96 |
+
|
97 |
+
if "groceries" in st.session_state and st.session_state["groceries"]:
|
98 |
+
st.session_state.messages.append(
|
99 |
+
ChatMessage(
|
100 |
+
role="user",
|
101 |
+
content=f"Using these items: `{st.session_state['groceries']}` "
|
102 |
+
f"generate a recipe.",
|
103 |
+
)
|
104 |
+
)
|
105 |
+
with st.chat_message("assistant"):
|
106 |
+
stream_handler = StreamHandler(st.empty())
|
107 |
+
llm = ChatOpenAI(
|
108 |
+
openai_api_key=openai_api_key, streaming=True, callbacks=[stream_handler]
|
109 |
+
)
|
110 |
+
response = llm(st.session_state.messages)
|
111 |
+
st.session_state.messages.append(
|
112 |
+
ChatMessage(role="assistant", content=response.content)
|
113 |
+
)
|
114 |
+
st.session_state["groceries"] = ""
|
env.example
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
MODEL_NAME=salesforce/blip:2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746
|
2 |
+
REPLICATE_API_TOKEN=
|
3 |
+
OPENAI_API_KEY=
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
replicate
|
2 |
+
streamlit
|
3 |
+
langchain
|
4 |
+
python-dotenv
|