papasega commited on
Commit
6f91c68
1 Parent(s): 26ae605

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from typing import Any, Dict, List, Tuple
3
+ from llama_index import BaseAgent # Assurez-vous d'importer correctement votre BaseAgent
4
+ from ansi2html import Ansi2HTMLConverter
5
+ import gradio as gr
6
+ from gradio.themes.utils import colors, fonts, sizes
7
+
8
+ class GradioAgentChatPack:
9
+ """Gradio chatbot to chat with your own Agent."""
10
+
11
+ def __init__(self, agent: BaseAgent, **kwargs: Any) -> None:
12
+ """Init params."""
13
+ self.agent = agent
14
+ self.thoughts = ""
15
+ self.conv = Ansi2HTMLConverter()
16
+
17
+ def get_modules(self) -> Dict[str, Any]:
18
+ """Get modules."""
19
+ return {"agent": self.agent}
20
+
21
+ def _handle_user_message(self, user_message, history):
22
+ """Handle the user submitted message. Clear message box, and append to the history."""
23
+ return "", [*history, (user_message, "")]
24
+
25
+ def _generate_response(self, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
26
+ """Generate the response from agent, and capture the stdout of the ReActAgent's thoughts."""
27
+ with Capturing() as output:
28
+ response = self.agent.stream_chat(chat_history[-1][0])
29
+ ansi = "\n========\n".join(output)
30
+ html_output = self.conv.convert(ansi)
31
+ for token in response.response_gen:
32
+ chat_history[-1][1] += token
33
+ yield chat_history, str(html_output)
34
+
35
+ def _reset_chat(self) -> Tuple[str, str]:
36
+ """Reset the agent's chat history. And clear all dialogue boxes."""
37
+ self.agent.reset()
38
+ return "", "", "" # clear textboxes
39
+
40
+ def run(self, *args: Any, **kwargs: Any) -> Any:
41
+ """Run the pipeline."""
42
+ llama_theme = gr.themes.Soft(
43
+ primary_hue=colors.purple,
44
+ secondary_hue=colors.pink,
45
+ neutral_hue=colors.gray,
46
+ spacing_size=sizes.spacing_md,
47
+ radius_size=sizes.radius_md,
48
+ text_size=sizes.text_lg,
49
+ font=(
50
+ fonts.GoogleFont("Quicksand"),
51
+ "ui-sans-serif",
52
+ "sans-serif",
53
+ ),
54
+ font_mono=(
55
+ fonts.GoogleFont("IBM Plex Mono"),
56
+ "ui-monospace",
57
+ "monospace",
58
+ ),
59
+ )
60
+ llama_theme.set(
61
+ body_background_fill="#FFFFFF",
62
+ body_background_fill_dark="#000000",
63
+ button_primary_background_fill="linear-gradient(90deg, *primary_300, *secondary_400)",
64
+ button_primary_background_fill_hover="linear-gradient(90deg, *primary_200, *secondary_300)",
65
+ button_primary_text_color="white",
66
+ button_primary_background_fill_dark="linear-gradient(90deg, *primary_600, *secondary_800)",
67
+ slider_color="*secondary_300",
68
+ slider_color_dark="*secondary_600",
69
+ block_title_text_weight="600",
70
+ block_border_width="3px",
71
+ block_shadow="*shadow_drop_lg",
72
+ button_shadow="*shadow_drop_lg",
73
+ button_large_padding="32px",
74
+ )
75
+
76
+ demo = gr.Blocks(
77
+ theme=llama_theme,
78
+ css="#box { height: 420px; overflow-y: scroll !important} #logo { align-self: right }",
79
+ )
80
+ with demo:
81
+ with gr.Row():
82
+ gr.Markdown(
83
+ "# Gradio Chat With Your Agent Powered by LlamaIndex and LlamaHub 🦙\n"
84
+ "This Gradio app allows you to chat with your own agent (`BaseAgent`).\n"
85
+ )
86
+ gr.Markdown(
87
+ "[![Alt text](https://d3ddy8balm3goa.cloudfront.net/other/llama-index-light-transparent-sm-font.svg)](https://llamaindex.ai)",
88
+ elem_id="logo",
89
+ )
90
+ with gr.Row():
91
+ chat_window = gr.Chatbot(
92
+ label="Message History",
93
+ scale=3,
94
+ )
95
+ console = gr.HTML(elem_id="box")
96
+ with gr.Row():
97
+ message = gr.Textbox(label="Write A Message", scale=4)
98
+ clear = gr.ClearButton()
99
+
100
+ message.submit(
101
+ self._handle_user_message,
102
+ [message, chat_window],
103
+ [message, chat_window],
104
+ queue=False,
105
+ ).then(
106
+ self._generate_response,
107
+ chat_window,
108
+ [chat_window, console],
109
+ )
110
+ clear.click(self._reset_chat, None, [message, chat_window, console])
111
+
112
+ demo.launch(server_name="0.0.0.0", server_port=8080)
113
+
114
+ # Note: Replace `BaseAgent` with your actual agent class and ensure it has the necessary methods.