Bazedgul's picture
Create app.py
f0f4259
import os
from typing import Optional, Tuple
import gradio as gr
from langchain.chains import ConversationChain
from langchain.llms import OpenAI
def load_chain():
"""Logic for loading the chain you want to use should go here."""
llm = OpenAI(temperature=0)
chain = ConversationChain(llm=llm)
return chain
def set_openai_api_key(api_key: str):
"""Set the api key and return chain.
If no api_key, then None is returned.
"""
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
chain = load_chain()
os.environ["OPENAI_API_KEY"] = ""
return chain
def chat(
inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
):
"""Execute the chat functionality."""
history = history or []
# If chain is None, that is because no API key was provided.
if chain is None:
history.append((inp, "Please paste your OpenAI key to use"))
return history, history
# Run chain and append input.
output = chain.run(input=inp)
history.append((inp, output))
return history, history
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
with block:
with gr.Row():
gr.Markdown("<h3><center>LangChain Demo</center></h3>")
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key (sk-...)",
show_label=False,
lines=1,
type="password",
)
chatbot = gr.Chatbot()
with gr.Row():
message = gr.Textbox(
label="What's your question?",
placeholder="What's the answer to life, the universe, and everything?",
lines=1,
)
submit = gr.Button(value="Send", variant="secondary").style(full_width=False)
gr.Examples(
examples=[
"Hi! How's it going?",
"What should I do tonight?",
"Whats 2 + 2?",
],
inputs=message,
)
gr.HTML("Demo application of a LangChain chain.")
gr.HTML(
"<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain πŸ¦œοΈπŸ”—</a></center>"
)
state = gr.State()
agent_state = gr.State()
submit.click(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
message.submit(chat, inputs=[message, state, agent_state], outputs=[chatbot, state])
openai_api_key_textbox.change(
set_openai_api_key,
inputs=[openai_api_key_textbox],
outputs=[agent_state],
)
block.launch(debug=True)