File size: 5,850 Bytes
b20698b
d0bbed8
 
b20698b
 
 
 
d0bbed8
 
 
b20698b
d0bbed8
 
b20698b
8c08cae
d0bbed8
 
 
8c08cae
d0bbed8
 
8c08cae
d0bbed8
 
 
 
 
 
8c08cae
d0bbed8
8c08cae
 
 
 
 
 
 
 
d0bbed8
 
 
 
8c08cae
d0bbed8
 
 
 
 
8c08cae
d0bbed8
 
 
 
 
8c08cae
d0bbed8
 
8c08cae
d0bbed8
 
 
 
 
 
b20698b
d0bbed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b20698b
 
d0bbed8
 
 
 
 
 
 
 
b20698b
8c08cae
d0bbed8
b20698b
 
 
d0bbed8
 
 
b20698b
d0bbed8
 
 
b20698b
 
 
 
d0bbed8
8c08cae
 
b20698b
8c08cae
d0bbed8
 
 
b20698b
 
 
d0bbed8
 
 
 
8c08cae
d0bbed8
 
 
 
8c08cae
d0bbed8
 
 
 
 
 
b20698b
d0bbed8
 
 
 
b20698b
d0bbed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c08cae
b20698b
d0bbed8
b20698b
d0bbed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c08cae
d0bbed8
 
 
b20698b
 
d0bbed8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b20698b
 
 
d0bbed8
 
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
"""
Farmer.Chat Backend - FastAPI Application
Deploy to Hugging Face Space: https://huggingface.co/spaces/aakashdg/farmer-chat-backend
"""

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel, Field
from typing import Optional, Dict, Any
import os
import asyncio
import time
from datetime import datetime

# Import pipeline components
from src.pipeline import FarmerChatPipeline
from src.pdf_generator import generate_pdf_report

from openai import OpenAI
import httpx

# Initialize FastAPI
app = FastAPI(
    title="Farmer.Chat Backend",
    description="Multi-stage MCP pipeline for agricultural intelligence",
    version="2.0.0"
)

# CORS - Allow all origins for demo (restrict in production)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Initialize OpenAI client with FIXED httpx configuration
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
if not OPENAI_API_KEY:
    raise ValueError("OPENAI_API_KEY environment variable not set!")

# FIX: Create httpx client without proxy support
http_client = httpx.Client(
    timeout=httpx.Timeout(60.0),
    limits=httpx.Limits(max_keepalive_connections=5, max_connections=10)
)

# Initialize OpenAI with custom http client (bypasses proxy issues)
openai_client = OpenAI(
    api_key=OPENAI_API_KEY,
    http_client=http_client
)

print("✅ OpenAI client initialized with custom httpx client")
print(f"   Model: gpt-4o")

# Default location (Bangalore Agricultural Region)
DEFAULT_LOCATION = {
    "name": "Bangalore Agricultural Region",
    "lat": 12.8716,
    "lon": 77.4946
}

# Initialize pipeline
pipeline = FarmerChatPipeline(openai_client, DEFAULT_LOCATION)

# Request/Response Models
class QueryRequest(BaseModel):
    query: str = Field(..., min_length=3, max_length=500, description="Farmer's question")
    location: Optional[Dict[str, Any]] = Field(None, description="Custom location (lat, lon, name)")
    
    class Config:
        json_schema_extra = {
            "example": {
                "query": "Should I plant rice today?",
                "location": {
                    "name": "Bangalore",
                    "lat": 12.8716,
                    "lon": 77.4946
                }
            }
        }


class QueryResponse(BaseModel):
    success: bool
    query: str
    advice: str
    routing: Dict[str, Any]
    data: Dict[str, Any]
    execution_time_seconds: float
    timestamp: str


# Health check
@app.get("/")
async def root():
    return {
        "service": "Farmer.Chat Backend",
        "status": "operational",
        "version": "2.0.0",
        "endpoints": {
            "query": "/api/query",
            "health": "/api/health",
            "servers": "/api/servers"
        }
    }


@app.get("/api/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "timestamp": datetime.now().isoformat(),
        "openai_configured": bool(OPENAI_API_KEY),
        "location": DEFAULT_LOCATION
    }


@app.get("/api/servers")
async def list_servers():
    """List available MCP servers"""
    from src.executor import MCP_SERVER_REGISTRY
    
    return {
        "total_servers": len(MCP_SERVER_REGISTRY),
        "servers": MCP_SERVER_REGISTRY
    }


@app.post("/api/query", response_model=QueryResponse)
async def process_query(request: QueryRequest):
    """
    Main query endpoint - processes farmer questions through MCP pipeline
    """
    try:
        start_time = time.time()
        
        # Use custom location if provided, otherwise default
        location = request.location if request.location else DEFAULT_LOCATION
        
        # Update pipeline location if changed
        if request.location:
            pipeline.location = location
        
        # Process query through pipeline
        result = await pipeline.process_query(request.query, verbose=False)
        
        execution_time = time.time() - start_time
        
        return QueryResponse(
            success=True,
            query=request.query,
            advice=result["advice"],
            routing=result["routing"],
            data=result["compiled_data"],
            execution_time_seconds=round(execution_time, 2),
            timestamp=datetime.now().isoformat()
        )
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/api/export-pdf")
async def export_pdf(request: QueryRequest):
    """
    Export query result as PDF
    """
    try:
        # Process query
        result = await pipeline.process_query(request.query, verbose=False)
        
        # Generate PDF
        pdf_path = generate_pdf_report(
            query=request.query,
            advice=result["advice"],
            data=result["compiled_data"],
            location=pipeline.location
        )
        
        # Return PDF file
        return FileResponse(
            pdf_path,
            media_type="application/pdf",
            filename=f"farmer-chat-report-{int(time.time())}.pdf"
        )
        
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


# Error handlers
@app.exception_handler(404)
async def not_found_handler(request, exc):
    return JSONResponse(
        status_code=404,
        content={"error": "Endpoint not found", "path": str(request.url)}
    )


@app.exception_handler(500)
async def server_error_handler(request, exc):
    return JSONResponse(
        status_code=500,
        content={"error": "Internal server error", "detail": str(exc)}
    )


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)