Eladlev commited on
Commit
2e4600f
·
verified ·
1 Parent(s): 540d72d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +156 -0
  2. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.agents import create_tool_calling_agent
2
+ from langchain.agents import AgentExecutor
3
+ import os
4
+ from langchain_openai import ChatOpenAI
5
+ from langchain.agents import Tool
6
+ from langchain_community.utilities import GoogleSerperAPIWrapper
7
+ from langchain_core.prompts import ChatPromptTemplate
8
+ from langchain_core.messages import HumanMessage, AIMessage
9
+ import base64
10
+ from PIL import Image
11
+ import io
12
+
13
+
14
+ def encode_image(image_path):
15
+ with open(image_path, "rb") as image_file:
16
+ return base64.b64encode(image_file.read()).decode('utf-8')
17
+
18
+ os.environ["SERPER_API_KEY"] = '23'
19
+ os.environ['OPENAI_API_KEY'] = "skc"
20
+
21
+ llm = ChatOpenAI(temperature=0, model_name='gpt-4o', openai_api_key=os.environ['OPENAI_API_KEY'])
22
+ search = GoogleSerperAPIWrapper()
23
+ tools = [
24
+ Tool(
25
+ name="web_search",
26
+ func=search.run,
27
+ description="useful for when you need to extract **updated** information from the web"
28
+ )
29
+ ]
30
+
31
+ # prompt = ChatPromptTemplate.from_messages([
32
+ # self.system_prompt,
33
+ # self.source_prompt,
34
+ # self.generate_eval_message(url)])
35
+
36
+ agent_prompt = ChatPromptTemplate.from_messages(
37
+ [
38
+ (
39
+ "system",
40
+ "You are a helpful assistant. You are provided with an image an image and a question about the image. You should answer the question. You should use the Web search tool to find the most updated information.",
41
+ ),
42
+ ("human", "placeholder"),
43
+ ("placeholder", "{chat_history}"),
44
+ ("human", "{input}"),
45
+ ("placeholder", "{agent_scratchpad}"),
46
+ ]
47
+ )
48
+
49
+ agent = create_tool_calling_agent(llm, tools, agent_prompt)
50
+
51
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
52
+
53
+ import gradio as gr
54
+ import os
55
+ from openai import OpenAI
56
+
57
+ with gr.Blocks() as demo:
58
+ with gr.Row():
59
+ image = gr.Image(label="image", height=600)
60
+ chatbot = gr.Chatbot()
61
+
62
+ prompt = gr.Textbox(label="prompt")
63
+ serper_api = gr.Textbox(label="Serper API key")
64
+ openai_key = gr.Textbox(label="OpenAI API key")
65
+ gr.Examples(
66
+ examples=[
67
+ ["https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_sketch.png",
68
+ "Describe what is in the image",
69
+ "https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_sketch.png"]
70
+ ],
71
+ inputs=[image, prompt],
72
+ )
73
+
74
+
75
+ def respond(message, chat_history, image):
76
+ # Convert NumPy array to an Image object
77
+
78
+
79
+
80
+ agent_input_history = []
81
+ for c in chat_history:
82
+ agent_input_history.extend([HumanMessage(content=c[0]), AIMessage(content=c[1])])
83
+
84
+ out = agent_executor.invoke(
85
+ {
86
+ "input": message,
87
+ "chat_history": agent_input_history,
88
+ }
89
+ )
90
+
91
+ chat_history.append((message, out['output']))
92
+ return "", chat_history
93
+
94
+
95
+ def update_serper_api(serper_api):
96
+ os.environ["SERPER_API_KEY"] = serper_api
97
+ search = GoogleSerperAPIWrapper()
98
+ global tools
99
+ tools = [
100
+ Tool(
101
+ name="Web search",
102
+ func=search.run,
103
+ description="useful for when you need to extract **updated** information from the web"
104
+ )
105
+ ]
106
+ agent = create_tool_calling_agent(llm, tools, agent_prompt)
107
+ global agent_executor
108
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
109
+
110
+
111
+ def update_agent(openai_key):
112
+ os.environ['OPENAI_API_KEY'] = openai_key
113
+ llm = ChatOpenAI(temperature=0, model_name='gpt-4o', openai_api_key=os.environ['OPENAI_API_KEY'])
114
+ agent = create_tool_calling_agent(llm, tools, agent_prompt)
115
+ global agent_executor
116
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
117
+
118
+ def change_image(image):
119
+ image_pil = Image.fromarray(image)
120
+
121
+ # Save the image to a bytes buffer
122
+ buffer = io.BytesIO()
123
+ image_pil.save(buffer, format="PNG") # You can also use "JPEG" if needed
124
+
125
+ # Get the byte data from the buffer and encode it to base64
126
+ image_bytes = buffer.getvalue()
127
+ image_base64 = base64.b64encode(image_bytes).decode('utf-8')
128
+ message_content = [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,"
129
+ f"{image_base64}"}}]
130
+ image_message = HumanMessage(content=message_content)
131
+ global agent_prompt
132
+ agent_prompt = ChatPromptTemplate.from_messages(
133
+ [
134
+ (
135
+ "system",
136
+ "You are a helpful assistant. You are provided with an image an image and a question about the image. You should answer the question. You should use the Web search tool to find the most updated information.",
137
+ ),
138
+ image_message,
139
+ ("placeholder", "{chat_history}"),
140
+ ("human", "{input}"),
141
+ ("placeholder", "{agent_scratchpad}"),
142
+ ]
143
+ )
144
+
145
+
146
+
147
+ agent = create_tool_calling_agent(llm, tools, agent_prompt)
148
+ global agent_executor
149
+ agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
150
+
151
+
152
+ prompt.submit(respond, [prompt, chatbot, image], [prompt, chatbot])
153
+ openai_key.submit(update_agent, [openai_key], [])
154
+ serper_api.submit(update_serper_api, [serper_api], [])
155
+ image.change(change_image,[image],[])
156
+ demo.queue().launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tqdm==4.66.1
2
+ langchain==0.2.7
3
+ openai==1.35.10
4
+ tiktoken==0.7.0
5
+ easydict==1.11
6
+ sentence-transformers==2.2.2
7
+ langchain-google-genai==1.0.8
8
+ pillow==10.2.0
9
+ langchain_openai==0.1.20
10
+ langchain_community
11
+ gradio