Spaces:
Sleeping
Sleeping
File size: 12,645 Bytes
468ad97 10e9b7d 468ad97 10e9b7d 3c4371f 468ad97 a7035da 468ad97 a7035da 468ad97 a7035da 468ad97 a7035da 468ad97 4021bf3 468ad97 31243f4 468ad97 31243f4 468ad97 3c4371f 468ad97 eccf8e4 468ad97 7d65c66 468ad97 e80aab9 468ad97 e80aab9 468ad97 7d65c66 468ad97 e80aab9 468ad97 e80aab9 468ad97 e514fd7 468ad97 e514fd7 468ad97 e514fd7 468ad97 e80aab9 468ad97 e80aab9 468ad97 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
"""
Modified app.py - Fixed Gradio interface for RobotPai Agent
Uses Hugging Face models and fixes authentication issues
"""
import os
import sys
import gradio as gr
import pandas as pd
import logging
from typing import List, Tuple, Optional
import warnings
# Suppress warnings
warnings.filterwarnings("ignore")
logging.basicConfig(level=logging.ERROR)
# Disable LangSmith tracing globally to avoid authentication errors
os.environ["LANGCHAIN_TRACING_V2"] = "false"
try:
from agent import RobotPaiAgent, create_agent
print("β
Agent module imported successfully")
except ImportError as e:
print(f"β Failed to import agent: {e}")
print("Creating fallback agent...")
class FallbackAgent:
def process_query(self, query: str) -> str:
return f"π§ Agent setup incomplete. You asked: {query}\n\nPlease check:\n1. Environment variables are set\n2. Database is configured\n3. Dependencies are installed"
def add_documents(self, texts: List[str], metadatas: List = None) -> bool:
return False
def load_csv_for_analysis(self, file_path: str) -> bool:
return False
# Global agent instance
global_agent = None
def initialize_agent():
"""Initialize the RobotPai agent with error handling"""
global global_agent
try:
print("π€ Initializing RobotPai Agent...")
global_agent = create_agent()
if global_agent:
# Try to load existing CSV data
global_agent.load_csv_for_analysis("supabase_docs.csv")
print("β
Agent initialized successfully")
return "β
RobotPai Agent is ready! You can now ask questions about documents or CSV data."
else:
global_agent = FallbackAgent()
return "β οΈ Agent initialized in fallback mode. Some features may not work."
except Exception as e:
print(f"β Agent initialization failed: {e}")
global_agent = FallbackAgent()
return f"β Agent initialization failed: {str(e)}\n\nUsing fallback mode."
def chat_with_agent(message: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
"""Process chat message through the agent"""
if not message.strip():
return "", history
try:
if global_agent is None:
response = "π§ Agent not initialized. Please wait for setup to complete."
else:
# Process the query through the agent
response = global_agent.process_query(message)
# Add to history
history.append((message, response))
return "", history
except Exception as e:
error_response = f"β Error processing your message: {str(e)}"
history.append((message, error_response))
return "", history
def upload_and_process_file(file) -> str:
"""Handle file upload and processing"""
if file is None:
return "β No file uploaded"
try:
# Get file extension
file_name = file.name
file_path = file.name
if file_name.endswith('.csv'):
# Process CSV file
df = pd.read_csv(file_path)
# Add to agent if available
if global_agent and hasattr(global_agent, 'load_csv_for_analysis'):
success = global_agent.load_csv_for_analysis(file_path)
if success:
return f"β
CSV file processed successfully!\nπ {len(df)} rows, {len(df.columns)} columns\nπ You can now ask questions about this data."
else:
return f"β οΈ CSV file loaded but not added to vector store.\nπ {len(df)} rows, {len(df.columns)} columns"
else:
return f"π CSV file loaded: {len(df)} rows, {len(df.columns)} columns\nβ οΈ Vector store not available for indexing."
elif file_name.endswith('.txt'):
# Process text file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
if global_agent and hasattr(global_agent, 'add_documents'):
success = global_agent.add_documents([content], [{"source": file_name}])
if success:
return f"β
Text file processed and added to knowledge base!\nπ {len(content)} characters processed."
else:
return f"β οΈ Text file loaded but couldn't add to knowledge base.\nπ {len(content)} characters"
else:
return f"π Text file loaded: {len(content)} characters\nβ οΈ Vector store not available for indexing."
else:
return f"β Unsupported file type: {file_name}\nSupported types: .csv, .txt"
except Exception as e:
return f"β Error processing file: {str(e)}"
def get_system_status() -> str:
"""Get current system status"""
status_parts = []
# Check agent status
if global_agent:
if hasattr(global_agent, 'vectorstore') and global_agent.vectorstore:
status_parts.append("β
Vector Store: Connected")
else:
status_parts.append("β οΈ Vector Store: Not available")
if hasattr(global_agent, 'llm') and global_agent.llm:
status_parts.append("β
Language Model: Loaded")
else:
status_parts.append("β οΈ Language Model: Not available")
if hasattr(global_agent, 'supabase_client') and global_agent.supabase_client:
status_parts.append("β
Supabase: Connected")
else:
status_parts.append("β οΈ Supabase: Not connected")
else:
status_parts.append("β Agent: Not initialized")
# Check environment variables
required_vars = ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY"]
for var in required_vars:
if os.getenv(var):
status_parts.append(f"β
{var}: Set")
else:
status_parts.append(f"β {var}: Missing")
# Check CSV data
if os.path.exists("supabase_docs.csv"):
try:
df = pd.read_csv("supabase_docs.csv")
status_parts.append(f"β
CSV Data: {len(df)} rows available")
except:
status_parts.append("β οΈ CSV Data: File exists but couldn't read")
else:
status_parts.append("β οΈ CSV Data: No supabase_docs.csv found")
return "\n".join(status_parts)
def clear_chat() -> List:
"""Clear chat history"""
return []
def get_example_queries() -> List[str]:
"""Get example queries for users to try"""
return [
"What is RobotPai?",
"Search for information about Supabase",
"Analyze the CSV data - how many rows are there?",
"What columns are in the CSV file?",
"Show me the first few rows of data",
"Help me understand vector databases",
]
# Create Gradio interface
def create_interface():
"""Create the Gradio interface"""
with gr.Blocks(
title="π€ RobotPai - AI Assistant",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 1200px;
margin: auto;
}
.status-box {
background-color: #f0f0f0;
padding: 10px;
border-radius: 5px;
font-family: monospace;
font-size: 12px;
}
"""
) as demo:
gr.Markdown("""
# π€ RobotPai - AI Assistant
An intelligent assistant that can search documents, analyze CSV data, and answer questions using Hugging Face models.
**Features:**
- π Document search and Q&A
- π CSV data analysis
- π Vector-based similarity search
- π€ Powered by Hugging Face models
""")
with gr.Row():
with gr.Column(scale=2):
# Chat interface
chatbot = gr.Chatbot(
label="Chat with RobotPai",
height=400,
show_label=True,
container=True,
bubble_full_width=False
)
with gr.Row():
msg = gr.Textbox(
placeholder="Ask me anything about documents or data...",
label="Your Message",
scale=4
)
send_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Row():
clear_btn = gr.Button("Clear Chat", variant="secondary")
# Example queries
gr.Markdown("### π‘ Example Queries:")
with gr.Row():
for i, example in enumerate(get_example_queries()[:3]):
gr.Button(example, size="sm").click(
lambda x=example: (x, []), outputs=[msg, chatbot]
)
with gr.Row():
for i, example in enumerate(get_example_queries()[3:]):
gr.Button(example, size="sm").click(
lambda x=example: (x, []), outputs=[msg, chatbot]
)
with gr.Column(scale=1):
# File upload
gr.Markdown("### π File Upload")
file_upload = gr.File(
label="Upload CSV or TXT file",
file_types=[".csv", ".txt"],
type="filepath"
)
upload_status = gr.Textbox(
label="Upload Status",
interactive=False,
max_lines=5
)
# System status
gr.Markdown("### π§ System Status")
status_display = gr.Textbox(
label="Current Status",
interactive=False,
max_lines=10,
elem_classes=["status-box"]
)
refresh_status_btn = gr.Button("Refresh Status", variant="secondary")
# Setup initialization on load
demo.load(initialize_agent, outputs=[upload_status])
demo.load(get_system_status, outputs=[status_display])
# Event handlers
send_btn.click(
chat_with_agent,
inputs=[msg, chatbot],
outputs=[msg, chatbot]
)
msg.submit(
chat_with_agent,
inputs=[msg, chatbot],
outputs=[msg, chatbot]
)
clear_btn.click(clear_chat, outputs=[chatbot])
file_upload.change(
upload_and_process_file,
inputs=[file_upload],
outputs=[upload_status]
)
refresh_status_btn.click(
get_system_status,
outputs=[status_display]
)
return demo
# Launch the app
if __name__ == "__main__":
# Environment check
print("π Checking environment...")
required_vars = ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY"]
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
print(f"β οΈ Missing environment variables: {missing_vars}")
print("Please set these in your Hugging Face Space settings or .env file")
# Create and launch interface
try:
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False, # Set to True if you want a public link
show_error=True,
quiet=False
)
except Exception as e:
print(f"β Failed to launch app: {e}")
print("Try running with simpler configuration...")
# Fallback simple interface
def simple_chat(message):
return f"Echo: {message} (Fallback mode - please check setup)"
simple_demo = gr.Interface(
fn=simple_chat,
inputs=gr.Textbox(placeholder="Enter your message..."),
outputs=gr.Textbox(),
title="π€ RobotPai (Fallback Mode)",
description="The full interface failed to load. Please check your environment setup."
)
simple_demo.launch(server_name="0.0.0.0", server_port=7860) |