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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -2
app.py CHANGED
@@ -1,10 +1,22 @@
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
 
@@ -24,6 +36,19 @@ class GradioAgentChatPack:
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)
@@ -111,4 +136,7 @@ class GradioAgentChatPack:
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.
 
 
 
 
1
  # app.py
2
  from typing import Any, Dict, List, Tuple
 
3
  from ansi2html import Ansi2HTMLConverter
4
  import gradio as gr
5
  from gradio.themes.utils import colors, fonts, sizes
6
 
7
+ class BaseAgent:
8
+ """Dummy BaseAgent for demonstration purposes."""
9
+
10
+ def stream_chat(self, message: str):
11
+ class ResponseGen:
12
+ def __iter__(self):
13
+ for token in message:
14
+ yield token
15
+ return type("Response", (object,), {"response_gen": ResponseGen()})
16
+
17
+ def reset(self):
18
+ pass
19
+
20
  class GradioAgentChatPack:
21
  """Gradio chatbot to chat with your own Agent."""
22
 
 
36
 
37
  def _generate_response(self, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
38
  """Generate the response from agent, and capture the stdout of the ReActAgent's thoughts."""
39
+ class Capturing(list):
40
+ def __enter__(self):
41
+ self._stdout = sys.stdout
42
+ sys.stdout = self
43
+ return self
44
+ def __exit__(self, *args):
45
+ sys.stdout = self._stdout
46
+ def write(self, x):
47
+ self.append(x)
48
+ def flush(self):
49
+ pass
50
+
51
+ import sys
52
  with Capturing() as output:
53
  response = self.agent.stream_chat(chat_history[-1][0])
54
  ansi = "\n========\n".join(output)
 
136
 
137
  demo.launch(server_name="0.0.0.0", server_port=8080)
138
 
139
+ if __name__ == "__main__":
140
+ agent = BaseAgent() # Instantiate your agent here
141
+ chat_pack = GradioAgentChatPack(agent)
142
+ chat_pack.run()