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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
import time
|
5 |
+
import requests
|
6 |
+
import shutil
|
7 |
+
import json
|
8 |
+
|
9 |
+
from PIL import Image
|
10 |
+
from gradio_client import Client
|
11 |
+
from newsapi import NewsApiClient
|
12 |
+
|
13 |
+
# Import langchain things that are needed generically
|
14 |
+
from langchain import LLMMathChain, SerpAPIWrapper
|
15 |
+
from langchain.agents import AgentType, initialize_agent
|
16 |
+
from langchain.chat_models import ChatOpenAI
|
17 |
+
from langchain.tools import BaseTool, StructuredTool, Tool, tool
|
18 |
+
|
19 |
+
from langchain.tools import format_tool_to_openai_function
|
20 |
+
from langchain.schema import (
|
21 |
+
AIMessage,
|
22 |
+
HumanMessage,
|
23 |
+
SystemMessage
|
24 |
+
)
|
25 |
+
|
26 |
+
chat = ChatOpenAI(
|
27 |
+
openai_api_key=openai_api_key,
|
28 |
+
temperature=1.0,
|
29 |
+
streaming=True,
|
30 |
+
model='gpt-3.5-turbo-0613'
|
31 |
+
)
|
32 |
+
|
33 |
+
# import all defined functions, their definitions and a dictionary
|
34 |
+
from gpt_function_definitions import generate_image, generate_caption, get_news
|
35 |
+
|
36 |
+
|
37 |
+
#Streaming endpoint
|
38 |
+
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
39 |
+
|
40 |
+
# Get the value of the openai_api_key from environment variable
|
41 |
+
#openai_api_key = os.getenv("OPENAI_API_KEY")
|
42 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
43 |
+
|
44 |
+
|
45 |
+
# TOOLS , FUNCTION CALLING, AND AGENTS
|
46 |
+
# Load the tool configs that are needed.
|
47 |
+
# 'Tool' dataclass wraps functions that accept a single string input and returns a string output.
|
48 |
+
tools = [
|
49 |
+
Tool.from_function(
|
50 |
+
func=generate_image,
|
51 |
+
name="generate_image",
|
52 |
+
description="generate an image based on the prompt provided"
|
53 |
+
# coroutine= ... <- you can specify an async method if desired as well
|
54 |
+
),
|
55 |
+
#Tool.from_function(
|
56 |
+
# func=generate_music,
|
57 |
+
# name="generate_music",
|
58 |
+
# description="generate music based on an input text and input melody"
|
59 |
+
# # coroutine= ... <- you can specify an async method if desired as well
|
60 |
+
#),
|
61 |
+
Tool.from_function(
|
62 |
+
func=generate_caption,
|
63 |
+
name="generate_caption",
|
64 |
+
description="generate caption for the image present at the filepath provided"
|
65 |
+
# coroutine= ... <- you can specify an async method if desired as well
|
66 |
+
),
|
67 |
+
Tool.from_function(
|
68 |
+
func=get_news,
|
69 |
+
name="get_news",
|
70 |
+
description="get top three engilsh news items for a given query, sorted by relevancy"
|
71 |
+
# coroutine= ... <- you can specify an async method if desired as well
|
72 |
+
),]
|
73 |
+
|
74 |
+
# Creating OpenAI functions
|
75 |
+
# use LangChain tools as OpenAI functions.
|
76 |
+
functions = [format_tool_to_openai_function(t) for t in tools]
|
77 |
+
functions
|
78 |
+
|
79 |
+
# defining agents using tools and openai functions
|
80 |
+
agent = initialize_agent(tools, chat, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)
|
81 |
+
|
82 |
+
|
83 |
+
# function calling
|
84 |
+
def run_conversation(user_input):
|
85 |
+
# calling the agent
|
86 |
+
function_response = agent.run(user_input)
|
87 |
+
print(f"function_response is - {function_response}")
|
88 |
+
|
89 |
+
image_file_extns = ['png', 'jpg', 'gif', 'tiff', 'tif', 'svg', 'bmp']
|
90 |
+
literal_terms = ['caption', 'captions']
|
91 |
+
if any(extn in function_response for extn in image_file_extns) and not any(term in function_response for term in literal_terms) :
|
92 |
+
image_file = function_response.replace('sandbox:',"").split('(')[-1].split(')')[0]
|
93 |
+
print(f"image_file is -{image_file}")
|
94 |
+
return function_response, image_file
|
95 |
+
|
96 |
+
return function_response, None
|
97 |
+
|
98 |
+
|
99 |
+
system = SystemMessage(content = "You are a helpful AI assistant") # that translates English to Pirate English.")
|
100 |
+
|
101 |
+
def predict(user_input, temperature, stable_diff, image_cap, top_news, file_output, chatbot):
|
102 |
+
|
103 |
+
print(f"chatbot - {chatbot}")
|
104 |
+
print(f"user_input - {user_input}")
|
105 |
+
|
106 |
+
# file handling
|
107 |
+
print(f"Logging: files in the file directory is -{file_output}")
|
108 |
+
if file_output is not None:
|
109 |
+
files_avail = [f.name for f in file_output ]
|
110 |
+
print(f"files_available are -{files_avail} ")
|
111 |
+
else:
|
112 |
+
print("No files available at the moment!")
|
113 |
+
|
114 |
+
|
115 |
+
chat = ChatOpenAI(
|
116 |
+
openai_api_key=openai_api_key,
|
117 |
+
temperature=temperature, #1.0
|
118 |
+
streaming=True,
|
119 |
+
model='gpt-3.5-turbo-0613')
|
120 |
+
messages = [system]
|
121 |
+
plugins = [stable_diff, image_cap, top_news, ] #music_gen
|
122 |
+
function_call_decision = True if any(plugins) else False #"auto" if any(plugins) else "none"
|
123 |
+
|
124 |
+
if len(chatbot) != 0:
|
125 |
+
for conv in chatbot:
|
126 |
+
human = HumanMessage(content=conv[0])
|
127 |
+
ai = AIMessage(content=conv[1])
|
128 |
+
messages.append(human)
|
129 |
+
messages.append(ai)
|
130 |
+
messages.append(HumanMessage(content=user_input))
|
131 |
+
if function_call_decision:
|
132 |
+
# getting openAI function agent reponse
|
133 |
+
function_response, image_file = run_conversation(user_input)
|
134 |
+
gpt_response = AIMessage(content= function_response)
|
135 |
+
bot_message = gpt_response.content
|
136 |
+
print(f"bot_message - {bot_message}")
|
137 |
+
chatbot.append((user_input, bot_message))
|
138 |
+
return "", chatbot, image_file
|
139 |
+
else: # for first user message
|
140 |
+
#human = HumanMessage(content=user_input)
|
141 |
+
#messages.append(human)
|
142 |
+
messages.append(HumanMessage(content=user_input))
|
143 |
+
if function_call_decision:
|
144 |
+
# getting openAI function agent reponse
|
145 |
+
function_response, image_file = run_conversation(user_input)
|
146 |
+
gpt_response = AIMessage(content= function_response)
|
147 |
+
bot_message = gpt_response.content
|
148 |
+
print(f"bot_message - {bot_message}")
|
149 |
+
chatbot.append((user_input, bot_message))
|
150 |
+
return "", chatbot, image_file
|
151 |
+
|
152 |
+
print(f"messages - {messages}")
|
153 |
+
|
154 |
+
# getting gpt3.5's response
|
155 |
+
gpt_response = chat(messages)
|
156 |
+
print(f"gpt_response - {gpt_response}")
|
157 |
+
bot_message = gpt_response.content
|
158 |
+
print(f"bot_message - {bot_message}")
|
159 |
+
|
160 |
+
chatbot.append((user_input, bot_message))
|
161 |
+
|
162 |
+
return "", chatbot, None
|
163 |
+
|
164 |
+
|
165 |
+
|
166 |
+
def add_image(file_to_save, file_output):
|
167 |
+
print(f"image file_to_save is - {file_to_save}")
|
168 |
+
print(f"files available in directory are -{file_output}")
|
169 |
+
|
170 |
+
if file_output is not None:
|
171 |
+
file_output = [f.name for f in file_output]
|
172 |
+
if file_to_save is None:
|
173 |
+
return file_output
|
174 |
+
file_output = [file_to_save] if file_output is None else file_output + [file_to_save]
|
175 |
+
print(f"Logging: Updated file directory - {file_output}")
|
176 |
+
return file_output #gr.update(value="dog1.jpg")
|
177 |
+
|
178 |
+
def add_audio(file_to_save, file_output):
|
179 |
+
print(f"audio file_to_save is - {file_to_save}")
|
180 |
+
print(f"files available in directory are -{file_output}")
|
181 |
+
|
182 |
+
if file_output is not None:
|
183 |
+
file_output = [f.name for f in file_output]
|
184 |
+
if file_to_save is None:
|
185 |
+
return file_output
|
186 |
+
file_output = [file_to_save] if file_output is None else file_output + [file_to_save]
|
187 |
+
print(f"Logging: Updated file directory - {file_output}")
|
188 |
+
return file_output #gr.update(value="dog1.jpg")
|
189 |
+
|
190 |
+
def upload_file(file, file_output):
|
191 |
+
print(f"Logging: all files available - {file_output}")
|
192 |
+
print(f"Logging: file uploaded is - {file}")
|
193 |
+
|
194 |
+
img_orig_name = file.name.split('/')[-1]
|
195 |
+
shutil.copy2(file.name, img_orig_name)
|
196 |
+
|
197 |
+
file_output = [file] if file_output is None else file_output + [file]
|
198 |
+
file_output = [f.name for f in file_output]
|
199 |
+
print(f"Logging: Updated file list is - {file_output}")
|
200 |
+
return file_output
|
201 |
+
|
202 |
+
|
203 |
+
messaging = """
|
204 |
+
How does a Language Model like GPT makes discerning choices regarding which plugins to run? Well, this is done using the Language Model as a reasoning agent and allowing it to assess and process information intelligently.<br>
|
205 |
+
<b>Langchain & OpenAI Function Calling</b>: AI models like gpt-3.5-turbo-0613 and gpt-4-0613, are designed to identify when and how to activate functions through API calls. These function-specific APIs generate a JSON object with necessary arguments, aiming to surpass the efficacy of traditional chat or text completion APIs.<br>
|
206 |
+
<b>Gradio Chatbots</b>: Gradio provides super easy way to build Chatbot UI. Refer our <a href="https://gradio.app/docs/#chatbot" target="_blank">Docs</a>. Using Langchain's OpenAI Functions Agent you can create chatbots designed to respond to queries by communicating with external APIs. The API responses are fed back to the Language Model for processing and a new response is generated for the user.The versatility of using Gradio to build LLM applications is immense. FOr example, in this Gradio app, you can have an array of Plugins based on functions which are tailored for various purposes (image, video, audio, text generation, utilities etc). This enhancing the breadth and depth of interactions with your Language Model.
|
207 |
+
"""
|
208 |
+
|
209 |
+
add_plugin_steps = """## Steps to add new Plugins to your Gradio ChatGPT Chatbot
|
210 |
+
|
211 |
+
1. **Acquire the API Endpoint**
|
212 |
+
- You need an API which you can query, and for this example let's consider using a text-to-speech demo hosted on Huggingface Spaces.
|
213 |
+
- **API Endpoint**: [https://gradio-neon-tts-plugin-coqui.hf.space/](https://gradio-neon-tts-plugin-coqui.hf.space/)
|
214 |
+
|
215 |
+
2. **Create a Function to Query the API**
|
216 |
+
- You can access any Gradio demo as an API via the Gradio Python Client.
|
217 |
+
```python
|
218 |
+
from gradio.client import Client
|
219 |
+
|
220 |
+
def texttospeech(input_text):
|
221 |
+
client = Client("https://gradio-neon-tts-plugin-coqui.hf.space/")
|
222 |
+
result = client.predict(
|
223 |
+
input_text, # str in 'Input' Textbox component
|
224 |
+
"en", # str in 'Language' Radio component
|
225 |
+
api_name="/predict"
|
226 |
+
)
|
227 |
+
return result
|
228 |
+
```
|
229 |
+
|
230 |
+
3. **Describe the Function to GPT-3.5**
|
231 |
+
- You need to describe your function to GPT3.5/4. This function definition will get passed to gpt and will suck up your token. GPT may or may not use this function based on user inputs later on.
|
232 |
+
- You can either use the Gradio demo for converting any given function to the required JSON format for GPT-3.5.
|
233 |
+
- Demo: [Function to JSON](https://huggingface.co/spaces/ysharma/function-to-JSON)
|
234 |
+
- Or, you can create the dictionary object on your own. Note that, the correct format is super important here.
|
235 |
+
- MAke sure to name your JSON object description as `<function_name>_func`.
|
236 |
+
```python
|
237 |
+
texttospeech_func = {
|
238 |
+
"name": "texttospeech",
|
239 |
+
"description": "generate speech from the given input text",
|
240 |
+
"parameters": {
|
241 |
+
"type": "object",
|
242 |
+
"properties": {
|
243 |
+
"input_text": {
|
244 |
+
"type": "string",
|
245 |
+
"description": "text that will be used to generate speech"
|
246 |
+
}
|
247 |
+
},
|
248 |
+
"required": [
|
249 |
+
"input_text"
|
250 |
+
]
|
251 |
+
}
|
252 |
+
}
|
253 |
+
```
|
254 |
+
|
255 |
+
4. **Add Function and JSON Object Details**
|
256 |
+
- Add the function definition and description to the `gpt_function_definitions.py` file (simply copy and paste).
|
257 |
+
- `dict_plugin_functions` is a dictionary of all available plugins. Add your plugin information to this dictionary in the required format.
|
258 |
+
```python
|
259 |
+
'texttospeech_func': {
|
260 |
+
'dict': texttospeech_func,
|
261 |
+
'func': texttospeech
|
262 |
+
}
|
263 |
+
```
|
264 |
+
|
265 |
+
5. **Update the Chatbot Layout**
|
266 |
+
- Go to the Blocks Chatbot layout and add a new checkbox for your plugin as:
|
267 |
+
```python
|
268 |
+
texttospeech = gr.Checkbox(label="📝🗣️Text-To-Speech", value=False)
|
269 |
+
```
|
270 |
+
- Add the new checkbox component to your submit and click events for your chatbot and to the predict function accordingly.
|
271 |
+
- And also to the `plugins` list in `predict`
|
272 |
+
```python
|
273 |
+
plugins = [music_gen, stable_diff, image_cap, top_news, texttospeech]
|
274 |
+
```
|
275 |
+
|
276 |
+
Thats it! you are have added your own brand new CHATGPT Plugin for yourself. Go PLAY!!
|
277 |
+
"""
|
278 |
+
|
279 |
+
|
280 |
+
|
281 |
+
# GRADIO BLOCK
|
282 |
+
with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
|
283 |
+
#chatbot {height: 520px; overflow: auto;}""") as demo: # #width: 1000px;
|
284 |
+
gr.HTML('<h1 align="center">Build 🚀ChatGPT🧩Plugin-UI using Langchain & Gradio</h1>')
|
285 |
+
with gr.Accordion("What is happening?", open=False):
|
286 |
+
gr.HTML(messaging)
|
287 |
+
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT-Plugins-UI-with-Langchain?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
|
288 |
+
with gr.Row():
|
289 |
+
with gr.Column():
|
290 |
+
openai_api_key_tb = gr.Textbox(label="Enter your OpenAI API key here",
|
291 |
+
value="🎁Keys are provided by HuggingFace for Free🥳 Don't need to enter yours!😉🙌",
|
292 |
+
container=False)
|
293 |
+
#plugin_message = gr.HTML()
|
294 |
+
with gr.Accordion("Plug-ins🛠️",open=True):
|
295 |
+
with gr.Row():
|
296 |
+
#music_gen = gr.Checkbox(label="🎵MusicGen", value=False)
|
297 |
+
stable_diff = gr.Checkbox(label="🖼️Diffusers", value=False)
|
298 |
+
image_cap = gr.Checkbox(label="🎨Describe Image", value=False)
|
299 |
+
top_news = gr.Checkbox(label="📰News", value=False)
|
300 |
+
#texttospeech = gr.Checkbox(label="📝🗣️Text-To-Speech", value=False)
|
301 |
+
#gr.CheckboxGroup(["🎵MusicGen", "🖼️Diffusers", "🎨Describe Image", "📰News", "📝🗣️Text-To-Speech" ], label="Plug-ins", info="enhance your ChatGPT experience using Plugins : Powered by Gradio!")
|
302 |
+
with gr.Column():
|
303 |
+
gen_image = gr.Image(label="generated image", type="filepath")
|
304 |
+
|
305 |
+
with gr.Row():
|
306 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
307 |
+
|
308 |
+
with gr.Row():
|
309 |
+
with gr.Column(scale=0.85):
|
310 |
+
inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter")
|
311 |
+
with gr.Column(scale=0.15, min_width=0):
|
312 |
+
btn = gr.UploadButton("📁Upload", file_types=["image", "audio"], file_count="single")
|
313 |
+
|
314 |
+
b1 = gr.Button("🏃Run")
|
315 |
+
|
316 |
+
with gr.Row():
|
317 |
+
with gr.Accordion("Parameters", open=False):
|
318 |
+
top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
319 |
+
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
320 |
+
with gr.Accordion("Available Files", open=False):
|
321 |
+
file_output = gr.File(file_count="multiple", file_types=["image", "audio"])
|
322 |
+
|
323 |
+
inputs.submit( predict,
|
324 |
+
[inputs, temperature, stable_diff, image_cap, top_news, file_output, chatbot],
|
325 |
+
[inputs, chatbot, gen_image ])
|
326 |
+
b1.click( predict,
|
327 |
+
[inputs, temperature, stable_diff, image_cap, top_news, file_output, chatbot],
|
328 |
+
[inputs, chatbot, gen_image ])
|
329 |
+
|
330 |
+
|
331 |
+
btn.upload(upload_file, [btn, file_output], file_output)
|
332 |
+
gen_image.change(add_image, [gen_image, file_output], file_output)
|
333 |
+
#gen_audio.change(add_audio, [gen_audio, file_output], file_output)
|
334 |
+
gr.HTML("""<a href="https://huggingface.co/spaces/ysharma/ChatGPT-Plugins-in-Gradio/blob/main/README.md" target="_blank">How to add new ChatGPT Plugins in Gradio Chatbot in 5 mins!! or open the accordion below.</a>""")
|
335 |
+
with gr.Accordion("How to add more Plugins to ChatGPT", open=False ):
|
336 |
+
gr.Markdown(add_plugin_steps)
|
337 |
+
|
338 |
+
#gr.Examples(
|
339 |
+
# inputs, top_p, temperature, openai_api_key, chat_counter, music_gen, stable_diff, image_cap, top_news, texttospeech, file_output, plugin_message, chatbot, state
|
340 |
+
#)
|
341 |
+
|
342 |
+
demo.queue().launch(debug=True, height = '1000')
|