Spaces:
Sleeping
Sleeping
File size: 15,544 Bytes
31881e7 2f8f181 31881e7 2f8f181 31881e7 2f8f181 31881e7 2f8f181 31881e7 2f8f181 31881e7 | 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | #!/usr/bin/env python3
"""
AutoGrantED Command Line Interface
Comprehensive management commands for the AutoGrantED system
"""
import click
import os
import sys
from datetime import datetime, timedelta
from flask import current_app
from flask.cli import with_appcontext
from werkzeug.security import generate_password_hash
# Add the app directory to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app, db
from app.models import User, GrantOpportunity, OpportunityScore, GrantApplication
from app.scrapers.sam_gov_scraper import SAMGovScraper
from app.analysis.scoring_engine import GrantScoringEngine
from app.generators.proposal_generator import ProposalGenerator
app = create_app()
@app.cli.command()
@click.option('--username', prompt=True, help='Admin username')
@click.option('--email', prompt=True, help='Admin email')
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True, help='Admin password')
@click.option('--company', prompt=True, help='Company name')
@click.option('--state', prompt=True, help='Company state (2-letter code)')
def create_admin(username, email, password, company, state):
"""Create an admin user."""
try:
# Check if user already exists
existing_user = User.query.filter_by(username=username).first()
if existing_user:
click.echo(f"β User '{username}' already exists!")
return
# Create new admin user
user = User(
username=username,
email=email,
company_name=company,
company_state=state.upper(),
sam_registered=True, # Assume admin has registrations
sba_registered=True
)
user.set_password(password)
db.session.add(user)
db.session.commit()
click.echo(f"β
Admin user '{username}' created successfully!")
click.echo(f" Company: {company}")
click.echo(f" State: {state.upper()}")
click.echo(f" Email: {email}")
except Exception as e:
click.echo(f"β Error creating admin user: {str(e)}")
db.session.rollback()
@app.cli.command()
@click.option('--source', default='sam_gov', help='Scraping source (sam_gov, grants_gov, sbir_gov)')
@click.option('--limit', default=50, help='Maximum opportunities to scrape')
def scan_opportunities(source, limit):
"""Scan for new grant opportunities."""
click.echo(f"π Scanning {source} for opportunities (limit: {limit})...")
try:
if source == 'sam_gov':
scraper = SAMGovScraper()
opportunities = scraper.scrape_opportunities()
new_count = 0
updated_count = 0
for opp_data in opportunities[:limit]:
existing = GrantOpportunity.query.filter_by(
solicitation_number=opp_data.get('solicitation_number')
).first()
if existing:
# Update existing opportunity
for key, value in opp_data.items():
if hasattr(existing, key):
setattr(existing, key, value)
existing.last_updated = datetime.utcnow()
updated_count += 1
else:
# Create new opportunity
opportunity = GrantOpportunity(**opp_data)
db.session.add(opportunity)
new_count += 1
db.session.commit()
click.echo(f"β
Scanning completed!")
click.echo(f" New opportunities: {new_count}")
click.echo(f" Updated opportunities: {updated_count}")
else:
click.echo(f"β Scraper '{source}' not implemented yet")
except Exception as e:
click.echo(f"β Error during scanning: {str(e)}")
db.session.rollback()
@app.cli.command()
@click.option('--user-id', type=int, help='Specific user ID to analyze for')
@click.option('--min-score', default=50, help='Minimum score threshold')
def analyze_opportunities(user_id, min_score):
"""Analyze opportunities and generate scores."""
click.echo(f"π§ Analyzing opportunities (min score: {min_score})...")
try:
# Get users to analyze for
if user_id:
users = [User.query.get(user_id)]
if not users[0]:
click.echo(f"β User with ID {user_id} not found")
return
else:
users = User.query.all()
if not users:
click.echo("β No users found")
return
# Get unprocessed opportunities
opportunities = GrantOpportunity.query.filter_by(processed=False).all()
if not opportunities:
click.echo("βΉοΈ No unprocessed opportunities found")
return
engine = GrantScoringEngine()
total_scores = 0
high_scores = 0
for user in users:
click.echo(f" Analyzing for user: {user.username}")
for opportunity in opportunities:
# Check if score already exists
existing_score = OpportunityScore.query.filter_by(
user_id=user.id,
opportunity_id=opportunity.id
).first()
if existing_score:
continue
# Generate score
result = engine.score_opportunity(user, opportunity)
# Save score
score = OpportunityScore(
user_id=user.id,
opportunity_id=opportunity.id,
agency_profile_score=result['agency_profile_score'],
market_need_score=result['market_need_score'],
competitive_landscape_score=result['competitive_landscape_score'],
total_score=result['total_score'],
agency_profile_reason=result['agency_profile_reason'],
market_need_reason=result['market_need_reason'],
competitive_landscape_reason=result['competitive_landscape_reason'],
ai_recommendation=result.get('ai_recommendation', ''),
confidence_level=result.get('confidence_level', 0.0),
model_version='gpt-4'
)
db.session.add(score)
total_scores += 1
if result['total_score'] >= min_score:
high_scores += 1
# Mark opportunities as processed
for opportunity in opportunities:
opportunity.processed = True
db.session.commit()
click.echo(f"β
Analysis completed!")
click.echo(f" Total scores generated: {total_scores}")
click.echo(f" High-scoring opportunities: {high_scores}")
except Exception as e:
click.echo(f"β Error during analysis: {str(e)}")
db.session.rollback()
@app.cli.command()
@click.option('--opportunity-id', type=int, required=True, help='Opportunity ID to generate proposal for')
@click.option('--user-id', type=int, required=True, help='User ID to generate proposal for')
def generate_proposal(opportunity_id, user_id):
"""Generate a proposal for a specific opportunity."""
click.echo(f"π Generating proposal for opportunity {opportunity_id}, user {user_id}...")
try:
# Get user and opportunity
user = User.query.get(user_id)
opportunity = GrantOpportunity.query.get(opportunity_id)
if not user:
click.echo(f"β User with ID {user_id} not found")
return
if not opportunity:
click.echo(f"β Opportunity with ID {opportunity_id} not found")
return
# Check if application already exists
existing_app = GrantApplication.query.filter_by(
user_id=user_id,
opportunity_id=opportunity_id
).first()
if existing_app:
click.echo(f"β οΈ Application already exists (ID: {existing_app.id})")
return
# Generate proposal
generator = ProposalGenerator()
proposal_data = generator.generate_complete_proposal(user, opportunity)
# Create application record
application = GrantApplication(
user_id=user_id,
opportunity_id=opportunity_id,
project_title=proposal_data['project_title'],
project_summary=proposal_data['project_summary'],
technical_narrative=proposal_data['technical_narrative'],
budget_narrative=proposal_data['budget_narrative'],
commercialization_plan=proposal_data.get('commercialization_plan', ''),
total_budget=proposal_data['budget']['total'],
personnel_costs=proposal_data['budget']['personnel'],
equipment_costs=proposal_data['budget']['equipment'],
travel_costs=proposal_data['budget']['travel'],
other_costs=proposal_data['budget']['other'],
indirect_costs=proposal_data['budget']['indirect']
)
db.session.add(application)
db.session.commit()
click.echo(f"β
Proposal generated successfully!")
click.echo(f" Application ID: {application.id}")
click.echo(f" Project Title: {proposal_data['project_title']}")
click.echo(f" Total Budget: ${proposal_data['budget']['total']:,}")
except Exception as e:
click.echo(f"β Error generating proposal: {str(e)}")
db.session.rollback()
@app.cli.command()
def system_status():
"""Display system status and statistics."""
click.echo("π AutoGrantED System Status")
click.echo("=" * 50)
try:
# Database statistics
user_count = User.query.count()
opportunity_count = GrantOpportunity.query.count()
active_opportunities = GrantOpportunity.query.filter(
GrantOpportunity.application_due_date > datetime.utcnow()
).count()
application_count = GrantApplication.query.count()
score_count = OpportunityScore.query.count()
click.echo(f"π₯ Users: {user_count}")
click.echo(f"π― Total Opportunities: {opportunity_count}")
click.echo(f"β° Active Opportunities: {active_opportunities}")
click.echo(f"π Applications: {application_count}")
click.echo(f"π§ Opportunity Scores: {score_count}")
# Recent activity
recent_opportunities = GrantOpportunity.query.filter(
GrantOpportunity.last_updated > datetime.utcnow() - timedelta(days=7)
).count()
recent_applications = GrantApplication.query.filter(
GrantApplication.created_at > datetime.utcnow() - timedelta(days=7)
).count()
click.echo(f"\nπ Recent Activity (7 days):")
click.echo(f" New/Updated Opportunities: {recent_opportunities}")
click.echo(f" New Applications: {recent_applications}")
# Top scoring opportunities
top_scores = db.session.query(OpportunityScore, GrantOpportunity).join(
GrantOpportunity
).order_by(OpportunityScore.total_score.desc()).limit(5).all()
if top_scores:
click.echo(f"\nπ Top Scoring Opportunities:")
for score, opp in top_scores:
click.echo(f" {score.total_score:3d} - {opp.title[:60]}...")
# System health checks
click.echo(f"\nπ§ System Health:")
# Check database connection
try:
db.session.execute('SELECT 1')
click.echo(" β
Database: Connected")
except:
click.echo(" β Database: Connection failed")
# Check for required environment variables
required_vars = ['OPENAI_API_KEY', 'SECRET_KEY']
for var in required_vars:
if os.environ.get(var):
click.echo(f" β
{var}: Set")
else:
click.echo(f" β οΈ {var}: Not set")
except Exception as e:
click.echo(f"β Error getting system status: {str(e)}")
@app.cli.command()
@click.option('--days', default=30, help='Number of days to keep')
def cleanup_old_data(days):
"""Clean up old data from the database."""
click.echo(f"π§Ή Cleaning up data older than {days} days...")
try:
cutoff_date = datetime.utcnow() - timedelta(days=days)
# Clean up old opportunities that are no longer active
old_opportunities = GrantOpportunity.query.filter(
GrantOpportunity.application_due_date < cutoff_date
).all()
deleted_count = 0
for opp in old_opportunities:
# Only delete if no applications exist
if not opp.applications.count():
db.session.delete(opp)
deleted_count += 1
db.session.commit()
click.echo(f"β
Cleanup completed!")
click.echo(f" Deleted {deleted_count} old opportunities")
except Exception as e:
click.echo(f"β Error during cleanup: {str(e)}")
db.session.rollback()
@app.cli.command()
def init_db():
"""Initialize the database with tables."""
click.echo("ποΈ Initializing database...")
try:
db.create_all()
click.echo("β
Database initialized successfully!")
except Exception as e:
click.echo(f"β Error initializing database: {str(e)}")
@app.cli.command()
@click.option('--test-gemini', is_flag=True, help='Test Gemini CLI connection')
@click.option('--test-scraping', is_flag=True, help='Test web scraping capabilities')
def test_system(test_gemini, test_scraping):
"""Test system components."""
click.echo("π§ͺ Testing system components...")
if test_gemini:
click.echo(" Testing Gemini CLI...")
try:
# Since we're using the CLI, we can just check for the executable
import shutil
if shutil.which(current_app.config['GEMINI_CLI_PATH']):
click.echo(" β
Gemini CLI: Executable found")
else:
click.echo(" β Gemini CLI: Executable not found")
except Exception as e:
click.echo(f" β Gemini CLI: {str(e)}")
if test_scraping:
click.echo(" Testing web scraping...")
try:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
driver.get('https://www.google.com')
driver.quit()
click.echo(" β
Web scraping: Working")
except Exception as e:
click.echo(f" β Web scraping: {str(e)}")
if __name__ == '__main__':
with app.app_context():
app.cli()
|