Julian Vanecek commited on
Commit
6749a1c
·
1 Parent(s): cb292fb

Adapt v3 to use working v2 patterns

Browse files

- Copy working structure from v2 example
- Use tuple-based chat history instead of dict format
- Simplify Gradio components (remove State, Accordion, etc)
- Use gradio>=4.0.0 instead of pinned version
- Remove complex event chaining with .then()
- Launch with server_name and server_port parameters

Files changed (3) hide show
  1. app.py +10 -24
  2. py/frontend/gradio_app_simple.py +196 -0
  3. requirements.txt +18 -1
app.py CHANGED
@@ -1,30 +1,16 @@
 
1
  """
2
- Minimal Gradio app for HuggingFace Spaces
3
  """
4
- import gradio as gr
5
- import os
6
 
7
- def chat_function(message, history):
8
- """Simple chat function that echoes the message"""
9
- response = f"I received your message: '{message}'. This is a test deployment."
10
- return response
11
 
12
- # Create interface
13
- with gr.Blocks() as demo:
14
- gr.Markdown("# AI Assistant Multi-Agent System")
15
- gr.Markdown("This is a simplified version for testing HuggingFace deployment.")
16
-
17
- chatbot = gr.Chatbot()
18
- msg = gr.Textbox()
19
- clear = gr.Button("Clear")
20
-
21
- def respond(message, chat_history):
22
- bot_message = chat_function(message, chat_history)
23
- chat_history.append((message, bot_message))
24
- return "", chat_history
25
-
26
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
27
- clear.click(lambda: None, None, chatbot, queue=False)
28
 
29
  if __name__ == "__main__":
30
- demo.launch()
 
1
+ #!/usr/bin/env python3
2
  """
3
+ Hugging Face Spaces App Entry Point
4
  """
 
 
5
 
6
+ import sys
7
+ from pathlib import Path
 
 
8
 
9
+ # Add current directory to path
10
+ sys.path.append(str(Path(__file__).parent))
11
+
12
+ # Import and run the simplified Gradio app
13
+ from py.frontend.gradio_app_simple import main
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  if __name__ == "__main__":
16
+ main()
py/frontend/gradio_app_simple.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Simplified Gradio Frontend for Multi-Agent System - HuggingFace Compatible
4
+ """
5
+
6
+ import gradio as gr
7
+ import sys
8
+ from pathlib import Path
9
+
10
+ # Add parent directory to path
11
+ sys.path.append(str(Path(__file__).parent.parent))
12
+
13
+ from backend.chat_history_manager import ChatHistoryManager
14
+ from backend.api_gateway import APIGateway
15
+ from config_loader import get_config
16
+ from tools import agent_tools
17
+
18
+ class SimpleAgentUI:
19
+ """Simplified Gradio UI for the multi-agent system."""
20
+
21
+ def __init__(self):
22
+ """Initialize the UI."""
23
+ # Load configuration
24
+ self.config = get_config()
25
+
26
+ # Initialize API Gateway
27
+ self.api_gateway = APIGateway()
28
+
29
+ # Initialize chat history manager
30
+ db_path = self.config.get("conversation.database_path")
31
+ self.history_manager = ChatHistoryManager(db_path)
32
+
33
+ # Initialize agents
34
+ self.current_agent = "document_reader"
35
+ self._initialize_agents()
36
+
37
+ # Get default settings
38
+ self.current_model = self.config.get_default_model()
39
+ self.current_product = self.config.get_default_product()
40
+ self.current_version = self.config.get_default_version(self.current_product)
41
+
42
+ def _initialize_agents(self):
43
+ """Initialize agents with current model."""
44
+ from langchain_openai import ChatOpenAI
45
+
46
+ # Create LLM
47
+ llm = ChatOpenAI(
48
+ model=self.current_model,
49
+ temperature=0.0,
50
+ max_tokens=4000
51
+ )
52
+
53
+ # Initialize agents
54
+ agent_tools.initialize_agents(llm, self.api_gateway)
55
+
56
+ def chat_response(self, message: str, history: list, model: str, product: str, version: str):
57
+ """Generate a chat response."""
58
+ if not message.strip():
59
+ return history
60
+
61
+ # Update settings if changed
62
+ if model != self.current_model:
63
+ self.current_model = model
64
+ self._initialize_agents()
65
+
66
+ self.current_product = product
67
+ self.current_version = version
68
+
69
+ try:
70
+ # Create context
71
+ agent_context = {
72
+ "product": self.current_product,
73
+ "version": self.current_version,
74
+ "model": self.current_model
75
+ }
76
+
77
+ # Run agent
78
+ result_dict = agent_tools.run_agent(self.current_agent, message, agent_context)
79
+
80
+ # Get response
81
+ response = result_dict.get("output", "No response generated")
82
+ new_agent_id = result_dict.get("agent_id", self.current_agent)
83
+
84
+ # Check if agent changed
85
+ if new_agent_id != self.current_agent:
86
+ self.current_agent = new_agent_id
87
+ response = f"[Switched to {new_agent_id}] {response}"
88
+
89
+ # Add agent prefix
90
+ agent_prefix = f"[{self.current_agent}]: "
91
+ response = agent_prefix + response
92
+
93
+ # Update history
94
+ history.append((message, response))
95
+
96
+ except Exception as e:
97
+ error_msg = f"Error: {str(e)}"
98
+ history.append((message, error_msg))
99
+
100
+ return history
101
+
102
+ def create_interface(self):
103
+ """Create the Gradio interface."""
104
+ with gr.Blocks(title="AI Assistant Multi-Agent System") as demo:
105
+ gr.Markdown("# 🤖 AI Assistant Multi-Agent System")
106
+ gr.Markdown("Chat with specialized agents for documentation, settings, and more.")
107
+
108
+ with gr.Row():
109
+ with gr.Column(scale=3):
110
+ chatbot = gr.Chatbot(
111
+ label="Chat History",
112
+ height=500
113
+ )
114
+
115
+ msg = gr.Textbox(
116
+ label="Your Message",
117
+ placeholder="Ask a question...",
118
+ lines=2
119
+ )
120
+
121
+ with gr.Row():
122
+ submit_btn = gr.Button("Send", variant="primary")
123
+ clear_btn = gr.Button("Clear")
124
+
125
+ with gr.Column(scale=1):
126
+ # Model selection
127
+ model_options = [(m["display_name"], m["model_id"])
128
+ for m in self.config.get_available_models()]
129
+ model_dropdown = gr.Dropdown(
130
+ choices=model_options,
131
+ value=self.current_model,
132
+ label="AI Model"
133
+ )
134
+
135
+ # Product selection
136
+ product_options = [(p["display_name"], p["id"])
137
+ for p in self.config.get_available_products()]
138
+ product_dropdown = gr.Dropdown(
139
+ choices=product_options,
140
+ value=self.current_product,
141
+ label="Product"
142
+ )
143
+
144
+ # Version selection
145
+ version_dropdown = gr.Dropdown(
146
+ choices=["1.8", "1.6", "1.5", "1.2"],
147
+ value=self.current_version,
148
+ label="Version"
149
+ )
150
+
151
+ # Current agent display
152
+ agent_display = gr.Markdown(f"**Active Agent:** {self.current_agent}")
153
+
154
+ # Event handlers
155
+ def respond(message, history, model, product, version):
156
+ """Handle chat response."""
157
+ updated_history = self.chat_response(message, history, model, product, version)
158
+ agent_status = f"**Active Agent:** {self.current_agent}"
159
+ return updated_history, "", agent_status
160
+
161
+ def clear_chat():
162
+ """Clear chat history."""
163
+ return [], ""
164
+
165
+ # Wire up events
166
+ submit_btn.click(
167
+ respond,
168
+ inputs=[msg, chatbot, model_dropdown, product_dropdown, version_dropdown],
169
+ outputs=[chatbot, msg, agent_display]
170
+ )
171
+
172
+ msg.submit(
173
+ respond,
174
+ inputs=[msg, chatbot, model_dropdown, product_dropdown, version_dropdown],
175
+ outputs=[chatbot, msg, agent_display]
176
+ )
177
+
178
+ clear_btn.click(
179
+ clear_chat,
180
+ outputs=[chatbot, msg]
181
+ )
182
+
183
+ return demo
184
+
185
+ def main():
186
+ """Main function to run the Gradio app."""
187
+ ui = SimpleAgentUI()
188
+ demo = ui.create_interface()
189
+ demo.launch(
190
+ server_name="0.0.0.0",
191
+ server_port=7860,
192
+ share=False
193
+ )
194
+
195
+ if __name__ == "__main__":
196
+ main()
requirements.txt CHANGED
@@ -1,2 +1,19 @@
1
  # Core dependencies
2
- gradio==4.19.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Core dependencies
2
+ openai>=1.50.0
3
+ gradio>=4.0.0
4
+ tiktoken>=0.5.0
5
+
6
+ # LangChain
7
+ langchain
8
+ langchain-openai
9
+ langchain-community
10
+
11
+ # Vector operations
12
+ numpy
13
+
14
+ # Utilities
15
+ python-dotenv
16
+ PyYAML
17
+ pydantic
18
+ pydantic-settings
19
+ SQLAlchemy