File size: 3,562 Bytes
17b60d4
 
 
 
 
 
 
 
 
6aefcc0
 
 
 
17b60d4
 
 
 
 
 
 
 
e6db76d
17b60d4
 
 
 
 
 
d27a461
17b60d4
 
d27a461
e6db76d
17b60d4
d27a461
17b60d4
 
 
 
d27a461
17b60d4
 
d27a461
e6db76d
17b60d4
d27a461
 
 
 
 
 
 
 
 
e6db76d
d27a461
 
 
 
 
 
 
 
 
 
e6db76d
d27a461
 
 
17b60d4
 
9fda4b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17b60d4
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
import { Client } from "@gradio/client";

const HF_SPACE = "AUXteam/tiny_factory";

export class GradioService {
  private static client: any = null;

  static async getClient() {
    if (!this.client) {
      // Use HF Token from environment if available (set via vite define or process.env)
      // For browser, we check if it was injected during build
      const token = (import.meta as any).env?.VITE_HF_TOKEN || null;
      this.client = await Client.connect(HF_SPACE, token ? { hf_token: token } : {});
    }
    return this.client;
  }

  static async identifyPersonas(context: string) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/identify_personas", [context]);
      return result.data[0];
    } catch (error) {
      console.error("Error identifying personas:", error);
      throw error;
    }
  }

  static async startSimulationAsync(simulationId: string, contentText: string, format: string = "text") {
    try {
      const client = await this.getClient();
      const result = await client.predict("/start_simulation_async", [simulationId, contentText, format]);
      return result.data[0];
    } catch (error) {
      console.error("Error starting simulation:", error);
      throw error;
    }
  }

  static async getSimulationStatus(simulationId: string) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/get_simulation_status", [simulationId]);
      return result.data[0];
    } catch (error) {
      console.error("Error getting simulation status:", error);
      throw error;
    }
  }

  static async generateVariants(contentText: string, numVariants: number = 3) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/generate_variants", [contentText, numVariants]);
      return result.data[0];
    } catch (error) {
      console.error("Error generating variants:", error);
      return ["Unable to generate variants at this time."];
    }
  }

  static async listSimulations() {
    try {
      const client = await this.getClient();
      const result = await client.predict("/list_simulations", []);
      return result.data[0];
    } catch (error) {
      console.error("Error listing simulations:", error);
      return [];
    }
  }

  static async generatePersonas(businessDescription: string, customerProfile: string, numPersonas: number = 1) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/generate_personas", [businessDescription, customerProfile, numPersonas, null]);
      return result.data[0];
    } 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) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/generate_social_network", [name, personaCount, networkType, focusGroupName]);
      return result.data[0];
    } catch (error) {
      console.error("Error generating social network:", error);
      throw error;
    }
  }

  static async getNetworkGraph(simulationId: string) {
    try {
      const client = await this.getClient();
      const result = await client.predict("/get_network_graph", [simulationId]);
      return result.data[0];
    } catch (error) {
      console.error("Error getting network graph:", error);
      throw error;
    }
  }
}