CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-gradio--client

JavaScript and TypeScript client library for interacting with Gradio APIs, providing methods to connect to, submit predictions to, and manage connections with Gradio applications.

Overall
score

96%

Overview
Eval results
Files

task.mdevals/scenario-7/

Session-Aware Prediction Tracker

Build a session-aware prediction tracker that maintains state across multiple API interactions with a Gradio application. The tracker should demonstrate proper session management by supporting custom session identifiers, tracking predictions within sessions, and handling session state.

Requirements

Create a module that provides functionality to:

  1. Connect with Custom Sessions: Connect to a Gradio application with the ability to specify a custom session identifier. If no custom identifier is provided, allow the default behavior.

  2. Track Predictions by Session: Track all predictions made within a specific session. Store prediction results along with their timestamps and associate them with the session identifier.

  3. Session State Handling: Support maintaining state between consecutive predictions in the same session. Enable passing state data from one prediction to the next when working with stateful Gradio applications.

  4. Session Reconnection: Implement functionality to reconnect to a previously used session by its identifier and retrieve the session's prediction history.

Implementation

@generates

API

/**
 * Configuration options for the SessionTracker
 */
export interface SessionTrackerConfig {
  appUrl: string;
  sessionHash?: string;
  hf_token?: string;
}

/**
 * Represents a tracked prediction with metadata
 */
export interface TrackedPrediction {
  timestamp: number;
  endpoint: string;
  input: any;
  output: any;
  sessionHash: string;
}

/**
 * Session-aware prediction tracker for Gradio applications
 */
export class SessionTracker {
  /**
   * Creates a new SessionTracker instance and connects to the Gradio app
   * @param config Configuration including app URL and optional session hash
   * @returns Promise resolving to SessionTracker instance
   */
  static create(config: SessionTrackerConfig): Promise<SessionTracker>;

  /**
   * Makes a prediction and tracks it in the session history
   * @param endpoint The API endpoint name or index
   * @param data The input data for the prediction
   * @param state Optional state data to maintain between predictions
   * @returns Promise resolving to the prediction result
   */
  predict(endpoint: string | number, data: any, state?: any): Promise<any>;

  /**
   * Gets the session identifier being used
   * @returns The current session hash
   */
  getSessionHash(): string;

  /**
   * Gets all predictions made in this session
   * @returns Array of tracked predictions
   */
  getSessionHistory(): TrackedPrediction[];

  /**
   * Reconnects to verify the session is still valid
   * @returns Promise resolving to connection status ("connected", "broken", or "changed")
   */
  reconnect(): Promise<string>;
}

Test Cases

  • When creating a SessionTracker without a custom session hash, it successfully connects and generates a unique session identifier @test

  • When creating a SessionTracker with a custom session hash, it uses the provided hash instead of generating one @test

  • When making multiple predictions in the same session, all predictions are tracked in the session history with correct timestamps and session hash @test

  • When calling getSessionHistory, it returns all predictions made in chronological order with complete metadata @test

Dependencies { .dependencies }

@gradio/client { .dependency }

Provides the client library for interacting with Gradio applications, including connection management and session handling.

Install with Tessl CLI

npx tessl i tessl/npm-gradio--client

tile.json