File size: 5,664 Bytes
9ce3455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { HfInference } from '@huggingface/inference';
import dotenv from 'dotenv';

dotenv.config();

class HuggingFaceService {
    constructor() {
        this.hf = new HfInference(process.env.HUGGINGFACE_API_KEY);
        this.model = 'mistralai/Mistral-7B-Instruct-v0.2';
    }

    /**

     * Analyze training data using Hugging Face

     */
    async analyzeTrainingData(documentText, rawData = null) {
        try {
            const prompt = `You are an expert data analyst for the National Disaster Management Authority (NDMA) of India.



Analyze the following disaster management training data and provide comprehensive insights in JSON format:



${documentText.substring(0, 3000)}



Provide the following in valid JSON format:

{

  "totalTrainings": <number>,

  "totalParticipants": <number>,

  "themeDistribution": {"earthquake": <count>, "flood": <count>, etc},

  "stateWiseCoverage": {"state": <count>},

  "averageCompletionRate": "<percentage>%",

  "gapAnalysis": {

    "underservedStates": ["state1", "state2"],

    "underservedThemes": ["theme1"],

    "criticalGaps": ["gap1", "gap2"]

  },

  "recommendations": ["rec1", "rec2", "rec3"],

  "keyInsights": ["insight1", "insight2", "insight3"]

}



Respond ONLY with valid JSON, no markdown or explanations.`;

            const response = await this.hf.textGeneration({
                model: this.model,
                inputs: prompt,
                parameters: {
                    max_new_tokens: 1000,
                    temperature: 0.7,
                    return_full_text: false
                }
            });

            let analysisText = response.generated_text.trim();

            // Clean up response
            analysisText = analysisText.replace(/```json\n?/g, '').replace(/```\n?/g, '').trim();

            // Try to parse JSON
            let analysis;
            try {
                analysis = JSON.parse(analysisText);
            } catch (parseError) {
                console.warn('JSON parsing failed, creating fallback analysis');
                analysis = this.createFallbackAnalysis(documentText, rawData);
            }

            return analysis;
        } catch (error) {
            console.error('Hugging Face analysis error:', error);
            throw new Error(`Analysis failed: ${error.message}`);
        }
    }

    /**

     * Generate executive summary

     */
    async generateExecutiveSummary(analysisResults) {
        try {
            const prompt = `Based on this disaster management training analysis, write a concise 3-paragraph executive summary for NDMA officials:



${JSON.stringify(analysisResults, null, 2)}



Write in formal, professional language suitable for government documentation.`;

            const response = await this.hf.textGeneration({
                model: this.model,
                inputs: prompt,
                parameters: {
                    max_new_tokens: 500,
                    temperature: 0.7,
                    return_full_text: false
                }
            });

            return response.generated_text.trim();
        } catch (error) {
            throw new Error(`Executive summary generation failed: ${error.message}`);
        }
    }

    /**

     * RAG-based question answering

     */
    async answerQuestion(question, context) {
        try {
            const prompt = `Context: ${context.substring(0, 2000)}



Question: ${question}



Provide a detailed, data-driven answer based on the context:`;

            const response = await this.hf.textGeneration({
                model: this.model,
                inputs: prompt,
                parameters: {
                    max_new_tokens: 300,
                    temperature: 0.7,
                    return_full_text: false
                }
            });

            return response.generated_text.trim();
        } catch (error) {
            throw new Error(`Question answering failed: ${error.message}`);
        }
    }

    /**

     * Fallback analysis if AI fails

     */
    createFallbackAnalysis(documentText, rawData) {
        const analysis = {
            totalTrainings: 0,
            totalParticipants: 0,
            themeDistribution: {},
            stateWiseCoverage: {},
            averageCompletionRate: "N/A",
            gapAnalysis: {
                underservedStates: [],
                underservedThemes: [],
                criticalGaps: ["Unable to perform detailed analysis - please check data format"]
            },
            recommendations: [
                "Ensure data is in proper format with clear headers",
                "Include all required fields: training ID, date, location, theme, participants",
                "Maintain consistent data entry standards"
            ],
            keyInsights: [
                "Data analysis completed - please review the uploaded document for details"
            ]
        };

        if (rawData && Array.isArray(rawData)) {
            analysis.totalTrainings = rawData.length;

            rawData.forEach(row => {
                const participantFields = ['participants', 'Participants', 'total_participants'];
                for (const field of participantFields) {
                    if (row[field]) {
                        analysis.totalParticipants += parseInt(row[field]) || 0;
                        break;
                    }
                }
            });
        }

        return analysis;
    }
}

export default new HuggingFaceService();