Alex Arvanitidis commited on
Commit
22b8631
·
1 Parent(s): a46e984

fix: add gradio ui react

Browse files
Files changed (1) hide show
  1. Gradio_UI_React.py +114 -0
Gradio_UI_React.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import functools
2
+ from typing import Optional, List, Any, Dict, Tuple
3
+
4
+ from llama_index.core.agent import ReActAgent
5
+ from llama_index.core.llama_pack import BaseLlamaPack
6
+ from llama_index.core.llms import LLM
7
+ from llama_index.packs.gradio_react_agent_chatbot.base import SUPPORTED_TOOLS, Capturing
8
+
9
+
10
+ class GradioReActAgentPack(BaseLlamaPack):
11
+ """Gradio chatbot to chat with a ReActAgent pack."""
12
+
13
+ def __init__(
14
+ self,
15
+ tools_list: Optional[List[str]] = list(SUPPORTED_TOOLS.keys()),
16
+ llm: LLM,
17
+ **kwargs: Any,
18
+ ) -> None:
19
+ """Init params."""
20
+ try:
21
+ from ansi2html import Ansi2HTMLConverter
22
+ except ImportError:
23
+ raise ImportError("Please install ansi2html via `pip install ansi2html`")
24
+
25
+ tools = []
26
+ for t in tools_list:
27
+ try:
28
+ tools.append(SUPPORTED_TOOLS[t]())
29
+ except KeyError:
30
+ raise KeyError(f"Tool {t} is not supported.")
31
+ self.tools = tools
32
+
33
+ self.llm = llm
34
+ self.agent = ReActAgent.from_tools(
35
+ tools=functools.reduce(
36
+ lambda x, y: x.to_tool_list() + y.to_tool_list(), self.tools
37
+ ),
38
+ llm=self.llm,
39
+ verbose=True,
40
+ )
41
+
42
+ self.thoughts = ""
43
+ self.conv = Ansi2HTMLConverter()
44
+
45
+ def get_modules(self) -> Dict[str, Any]:
46
+ """Get modules."""
47
+ return {"agent": self.agent, "llm": self.llm, "tools": self.tools}
48
+
49
+ def _handle_user_message(self, user_message, history):
50
+ """Handle the user submitted message. Clear message box, and append
51
+ to the history.
52
+ """
53
+ return "", [*history, (user_message, "")]
54
+
55
+ def _generate_response(
56
+ self, chat_history: List[Tuple[str, str]]
57
+ ) -> Tuple[str, List[Tuple[str, str]]]:
58
+ """Generate the response from agent, and capture the stdout of the
59
+ ReActAgent's thoughts.
60
+ """
61
+ with Capturing() as output:
62
+ response = self.agent.stream_chat(chat_history[-1][0])
63
+ ansi = "\n========\n".join(output)
64
+ html_output = self.conv.convert(ansi)
65
+ for token in response.response_gen:
66
+ chat_history[-1][1] += token
67
+ yield chat_history, str(html_output)
68
+
69
+ def _reset_chat(self) -> Tuple[str, str]:
70
+ """Reset the agent's chat history. And clear all dialogue boxes."""
71
+ # clear agent history
72
+ self.agent.reset()
73
+ return "", "", "" # clear textboxes
74
+
75
+ def run(self, *args: Any, **kwargs: Any) -> Any:
76
+ """Run the pipeline."""
77
+ import gradio as gr
78
+
79
+ demo = gr.Blocks(
80
+ theme="gstaff/xkcd",
81
+ css="#box { height: 420px; overflow-y: scroll !important}",
82
+ )
83
+ with demo:
84
+ gr.Markdown(
85
+ "# Gradio ReActAgent Powered by LlamaIndex and LlamaHub 🦙\n"
86
+ "This Gradio app is powered by LlamaIndex's `ReActAgent` with\n"
87
+ "OpenAI's GPT-4-Turbo as the LLM. The tools are listed below.\n"
88
+ "## Tools\n"
89
+ "- [ArxivToolSpec](https://llamahub.ai/l/tools-arxiv)\n"
90
+ "- [WikipediaToolSpec](https://llamahub.ai/l/tools-wikipedia)"
91
+ )
92
+ with gr.Row():
93
+ chat_window = gr.Chatbot(
94
+ label="Message History",
95
+ scale=3,
96
+ )
97
+ console = gr.HTML(elem_id="box")
98
+ with gr.Row():
99
+ message = gr.Textbox(label="Write A Message", scale=4)
100
+ clear = gr.ClearButton()
101
+
102
+ message.submit(
103
+ self._handle_user_message,
104
+ [message, chat_window],
105
+ [message, chat_window],
106
+ queue=False,
107
+ ).then(
108
+ self._generate_response,
109
+ chat_window,
110
+ [chat_window, console],
111
+ )
112
+ clear.click(self._reset_chat, None, [message, chat_window, console])
113
+
114
+ demo.launch(debug=True, share=True, **kwargs)