File size: 3,862 Bytes
c9dda84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Use same-origin FastAPI endpoints on the Hugging Face Space; the backend proxies upstream API calls.
const API_BASE_URL = "";

export class GradioService {
  private static getHeaders() {
    return {
      "Content-Type": "application/json",
      "Accept": "application/json"
    };
  }

  static async identifyPersonas(context: string) {
    // Deprecated? Just returns context for now to not break apps that might expect a string
    console.warn("identifyPersonas is no longer supported directly by the REST API.");
    return context;
  }

  static async startSimulationAsync(simulationId: string, contentText: string, format: string = "text") {
    try {
      // simulationId in the old code might have been the focus group ID.
      // Let's assume simulationId is the focus_group_id
      const payload = {
        focus_group_id: simulationId,
        content_type: format,
        content_payload: contentText,
        parameters: {}
      };

      const response = await fetch(`${API_BASE_URL}/api/v1/simulations`, {
        method: "POST",
        headers: this.getHeaders(),
        body: JSON.stringify(payload)
      });

      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }

      const data = await response.json();
      return data.job_id;
    } catch (error) {
      console.error("Error starting simulation:", error);
      throw error;
    }
  }

  static async getSimulationStatus(jobId: string) {
    try {
      const response = await fetch(`${API_BASE_URL}/api/v1/simulations/${jobId}`, {
        headers: this.getHeaders()
      });

      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error getting simulation status:", error);
      throw error;
    }
  }

  static async generateVariants(contentText: string, numVariants: number = 3) {
    // This endpoint doesn't exist in the openapi spec.
    console.warn("generateVariants is no longer supported by the REST API.");
    return ["Variant generation not supported."];
  }

  static async listSimulations() {
    try {
      // Returns focus groups from the personas endpoint
      const response = await fetch(`${API_BASE_URL}/api/v1/personas`, {
        headers: this.getHeaders()
      });

      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }

      const data = await response.json();
      return data.focus_groups || [];
    } catch (error) {
      console.error("Error listing simulations/personas:", error);
      return [];
    }
  }

  static async generatePersonas(businessDescription: string, customerProfile: string, numPersonas: number = 1) {
    try {
      const payload = {
        business_description: businessDescription,
        customer_profile: customerProfile,
        num_personas: numPersonas
      };

      const response = await fetch(`${API_BASE_URL}/api/v1/personas/generate`, {
        method: "POST",
        headers: this.getHeaders(),
        body: JSON.stringify(payload)
      });

      if (!response.ok) {
        throw new Error(`API error: ${response.status}`);
      }

      const data = await response.json();
      return data;
    } catch (error) {
      console.error("Error generating personas:", error);
      throw error;
    }
  }

  static async generateSocialNetwork(name: string, personaCount: number = 10, networkType: string = "scale_free", focusGroupName: string | null = null) {
    console.warn("generateSocialNetwork is subsumed by persona generation or not supported.");
    return { status: "Network generated" };
  }

  static async getNetworkGraph(simulationId: string) {
    // Not supported
    console.warn("getNetworkGraph is not supported by the REST API.");
    return null;
  }
}