|
import os |
|
from typing import Optional, Tuple |
|
import gradio as gr |
|
import argparse |
|
import datetime |
|
import pickle |
|
|
|
import dotenv |
|
import sys |
|
from io import StringIO |
|
import re |
|
dotenv.load_dotenv() |
|
|
|
from langchain.callbacks import get_openai_callback |
|
|
|
import hydra |
|
from omegaconf import DictConfig, open_dict, OmegaConf |
|
|
|
|
|
|
|
class ChatbotAgentGradio(): |
|
def __init__( |
|
self, |
|
config_name |
|
): |
|
|
|
config = OmegaConf.load(f'./config/{config_name}.yaml') |
|
self.chatbot = hydra.utils.instantiate(config.model, _convert_="partial") |
|
|
|
def chat(self, |
|
inp: str, |
|
history: Optional[Tuple[str, str]], |
|
): |
|
|
|
"""Method for integration with gradio Chatbot""" |
|
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====") |
|
print("inp: " + inp) |
|
history = history or [] |
|
|
|
output = self.chatbot.run(inp) |
|
|
|
|
|
history.append((inp, output)) |
|
|
|
return history, history |
|
|
|
def update_foo(self, widget, state): |
|
if widget: |
|
state = widget |
|
return state |
|
|
|
def launch_app(self): |
|
|
|
block = gr.Blocks(css=".gradio-container {background-color: lightgray}") |
|
|
|
with block: |
|
instance = gr.State() |
|
show_chain_state = gr.State(False) |
|
|
|
|
|
with gr.Row(): |
|
gr.Markdown("<h3><center>UNHCR</center></h3>") |
|
|
|
|
|
|
|
with gr.Row(): |
|
chatbot = gr.Chatbot() |
|
|
|
|
|
with gr.Row(): |
|
message = gr.Textbox( |
|
label="What's your question?", |
|
lines=1, |
|
) |
|
submit = gr.Button(value="Send", variant="secondary").style(full_width=False) |
|
|
|
|
|
state = gr.State() |
|
agent_state = gr.State() |
|
|
|
submit.click(self.chat, inputs=[message, state], outputs=[chatbot, state]) |
|
message.submit(self.chat, inputs=[message, state], outputs=[chatbot, state]) |
|
|
|
|
|
block.launch(debug=True, share=False, server_port=7861) |
|
|
|
|
|
|
|
def simple(config): |
|
config = OmegaConf.load(f'./config/{config}.yaml') |
|
chatbot = hydra.utils.instantiate(config.model, _convert_="partial") |
|
|
|
while True: |
|
inp = input("\nUser: ") |
|
print(chatbot.run(inp)) |
|
if __name__ == '__main__': |
|
|
|
|
|
app = ChatbotAgentGradio('conf_0.1') |
|
app.launch_app() |
|
|
|
|
|
|
|
|