Class: Rack::AI::Providers::HuggingFace

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/ai/providers/huggingface.rb

Constant Summary collapse

API_BASE_URL =
"https://api-inference.huggingface.co"

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#build_request_data, #healthy?, #validate!

Constructor Details

#initialize(config) ⇒ HuggingFace

Returns a new instance of HuggingFace.



13
14
15
16
# File 'lib/rack/ai/providers/huggingface.rb', line 13

def initialize(config)
  super
  @client = build_client
end

Instance Method Details

#analyze_patterns(data) ⇒ Object



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
# File 'lib/rack/ai/providers/huggingface.rb', line 71

def analyze_patterns(data)
  # Use a general text analysis approach
  text = "Analyze these web traffic patterns: #{data.to_json}"
  
  response = @client.post("models/facebook/bart-large-cnn") do |req|
    req.body = {
      inputs: text,
      parameters: {
        max_length: 200,
        min_length: 50
      }
    }.to_json
  end

  handle_api_error(response) unless response.success?
  
  result = JSON.parse(response.body)
  summary = result.first&.dig("summary_text") || ""
  
  {
    anomalies: extract_anomalies(summary),
    trends: extract_trends(summary),
    recommendations: extract_recommendations(summary),
    confidence: 0.7,
    provider: :huggingface
  }
end

#classify_request(request_data) ⇒ Object



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
# File 'lib/rack/ai/providers/huggingface.rb', line 18

def classify_request(request_data)
  # Use a text classification model for request analysis
  text = build_request_text(request_data)
  
  response = @client.post("models/microsoft/DialoGPT-medium") do |req|
    req.body = {
      inputs: "Classify this web request as human, bot, spam, or suspicious: #{text}",
      parameters: {
        max_length: 50,
        temperature: 0.1
      }
    }.to_json
  end

  handle_api_error(response) unless response.success?
  
  result = JSON.parse(response.body)
  generated_text = result.first&.dig("generated_text") || ""
  
  # Parse classification from generated text
  classification = extract_classification(generated_text)
  
  {
    classification: classification,
    confidence: 0.8, # HuggingFace doesn't always provide confidence scores
    reasoning: generated_text,
    provider: :huggingface
  }
end

#detect_anomalies(request_data) ⇒ Object



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
# File 'lib/rack/ai/providers/huggingface.rb', line 99

def detect_anomalies(request_data)
  # Use anomaly detection approach
  text = build_security_analysis_text(request_data)
  
  response = @client.post("models/microsoft/DialoGPT-medium") do |req|
    req.body = {
      inputs: "Security analysis: #{text}. Threat level (low/medium/high):",
      parameters: {
        max_length: 100,
        temperature: 0.1
      }
    }.to_json
  end

  handle_api_error(response) unless response.success?
  
  result = JSON.parse(response.body)
  analysis = result.first&.dig("generated_text") || ""
  
  {
    threat_level: extract_threat_level(analysis),
    anomalies: extract_security_anomalies(analysis),
    confidence: 0.7,
    provider: :huggingface
  }
end

#enhance_content(content, enhancement_type) ⇒ Object



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
# File 'lib/rack/ai/providers/huggingface.rb', line 126

def enhance_content(content, enhancement_type)
  model = case enhancement_type
          when :seo
            "models/facebook/bart-large-cnn"
          when :readability
            "models/facebook/bart-large"
          else
            "models/t5-base"
          end

  prompt = build_enhancement_prompt(content, enhancement_type)
  
  response = @client.post(model) do |req|
    req.body = {
      inputs: prompt,
      parameters: {
        max_length: 500,
        temperature: 0.3
      }
    }.to_json
  end

  handle_api_error(response) unless response.success?
  
  result = JSON.parse(response.body)
  enhanced = result.first&.dig("generated_text") || content
  
  {
    original_content: content,
    enhanced_content: enhanced,
    enhancement_type: enhancement_type,
    provider: :huggingface
  }
end

#moderate_content(content, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rack/ai/providers/huggingface.rb', line 48

def moderate_content(content, options = {})
  # Use toxicity detection model
  response = @client.post("models/unitary/toxic-bert") do |req|
    req.body = {
      inputs: content
    }.to_json
  end

  handle_api_error(response) unless response.success?
  
  result = JSON.parse(response.body)
  
  # HuggingFace toxicity models return classification results
  toxic_score = result.first&.find { |r| r["label"] == "TOXIC" }&.dig("score") || 0.0
  
  {
    flagged: toxic_score > 0.5,
    categories: { toxicity: toxic_score > 0.5 },
    category_scores: { toxicity: toxic_score },
    provider: :huggingface
  }
end

#pingObject



161
162
163
164
# File 'lib/rack/ai/providers/huggingface.rb', line 161

def ping
  response = @client.get("models")
  response.success?
end