-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwiba_api_example.r
More file actions
269 lines (230 loc) · 8.03 KB
/
wiba_api_example.r
File metadata and controls
269 lines (230 loc) · 8.03 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Install required packages if not already installed
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("httr")) install.packages("httr")
if (!require("jsonlite")) install.packages("jsonlite")
# Load required libraries
library(tidyverse)
library(httr)
library(jsonlite)
# Configuration
BASE_URL <- "http://wiba.dev/api" # Update to your actual server URL
API_TOKEN <- "your_api_token_here" # Add your API token
# Function to create segments
create_segments <- function(input_data, column_name, window_size = 3, api_token = API_TOKEN) {
url <- paste0(BASE_URL, "/create_segments")
# Convert data frame to CSV string
csv_data <- capture.output(write.csv(input_data, row.names = FALSE))
csv_string <- paste(csv_data, collapse = "\n")
payload <- list(
data = csv_string,
column_name = column_name,
window_size = window_size,
step_size = 1
)
response <- POST(
url,
body = payload,
encode = "json",
add_headers("X-API-Token" = api_token) # Add API token header
)
if (status_code(response) == 200) {
result <- fromJSON(rawToChar(response$content))
segments_df <- as.data.frame(result)
print("Created segments:")
print(head(segments_df))
return(segments_df)
} else {
# Enhanced error handling
error_message <- tryCatch({
error_content <- fromJSON(rawToChar(response$content))
paste("Error:", status_code(response), error_content$detail)
}, error = function(e) {
paste("Error:", status_code(response), "Could not parse error response")
})
stop(error_message)
}
}
# Function to detect arguments
wiba_detect <- function(df, api_token = API_TOKEN) {
url <- paste0(BASE_URL, "/detect")
df_tmp = df
if (!is.null(df$text_segment)) {
df_tmp$original_text <- df_tmp$text
df_tmp$text <- df_tmp$text_segment
}
# Convert data frame to CSV string
csv_string <- capture.output(write.csv(df_tmp, row.names = FALSE))
csv_string <- paste(csv_string, collapse = "\n")
payload <- list(texts = csv_string)
response <- POST(
url,
body = payload,
encode = "json",
add_headers("X-API-Token" = api_token) # Add API token header
)
if (status_code(response) == 200) {
result <- fromJSON(rawToChar(response$content))
return(as.data.frame(result))
} else {
# Enhanced error handling
error_message <- tryCatch({
error_content <- fromJSON(rawToChar(response$content))
paste("Error:", status_code(response), error_content$detail)
}, error = function(e) {
paste("Error:", status_code(response), "Could not parse error response")
})
stop(error_message)
}
}
# Function to extract topics
wiba_extract <- function(texts, api_token = API_TOKEN) {
url <- paste0(BASE_URL, "/extract")
payload <- list(texts = texts)
response <- POST(
url,
body = payload,
encode = "json",
add_headers("X-API-Token" = api_token) # Add API token header
)
if (status_code(response) == 200) {
result <- fromJSON(rawToChar(response$content))
return(as.data.frame(result))
} else {
# Enhanced error handling
error_message <- tryCatch({
error_content <- fromJSON(rawToChar(response$content))
paste("Error:", status_code(response), error_content$detail)
}, error = function(e) {
paste("Error:", status_code(response), "Could not parse error response")
})
stop(error_message)
}
}
# Function to analyze stance
wiba_stance <- function(texts, topics, api_token = API_TOKEN) {
url <- paste0(BASE_URL, "/stance")
payload <- list(
texts = texts,
topics = topics
)
response <- POST(
url,
body = payload,
encode = "json",
add_headers("X-API-Token" = api_token) # Add API token header
)
if (status_code(response) == 200) {
result <- fromJSON(rawToChar(response$content))
return(as.data.frame(result))
} else {
# Enhanced error handling
error_message <- tryCatch({
error_content <- fromJSON(rawToChar(response$content))
paste("Error:", status_code(response), error_content$detail)
}, error = function(e) {
paste("Error:", status_code(response), "Could not parse error response")
})
stop(error_message)
}
}
# Function to get a demo token if needed
get_demo_token <- function() {
url <- paste0(BASE_URL, "/demo-token")
response <- GET(url)
if (status_code(response) == 200) {
result <- fromJSON(rawToChar(response$content))
return(result$token)
} else {
stop(paste("Error getting demo token:", status_code(response)))
}
}
# Create example dataset
create_example_dataset <- function() {
data <- list(
id = as.character(1:21),
parent_id = c(NA, rep("1", 3), NA, rep("5", 2), NA, rep("8", 2), NA,
rep("11", 2), NA, rep("14", 2), NA, rep("17", 3), "20"),
thread_id = c(rep("1", 4), rep("5", 3), rep("8", 3), rep("11", 3),
rep("14", 3), rep("17", 5)),
text = c(
# Thread 1
"I strongly believe that this new policy will significantly reduce pollution...",
"While the goal of reducing pollution is certainly commendable...",
"I understand the concerns about economic costs...",
"I completely agree with you on the long-term perspective...",
# Thread 2
"The new law aimed at improving education outcomes...",
"While I agree that improving education is essential...",
"The city's new traffic management plan has sparked...",
# Thread 3
"The city's traffic management plan is flawed...",
"While the bike lanes cause issues...",
"We need better planning and enforcement...",
# Additional arguments
"Public transportation should be improved...",
"Subsidizing small businesses to comply...",
"Teacher training reforms will have...",
"I don't think the policy will reduce emissions...",
"Banning cars from the city center...",
"Encouraging carpooling would also reduce traffic...",
"The education reform law should focus...",
"Funding for renewable energy should be prioritized...",
"Tax incentives for green technologies...",
"Road pricing would better address congestion...",
"More public awareness campaigns are needed..."
)
)
return(as.data.frame(data))
}
# Example usage
main <- function(api_token = NULL) {
# Get a demo token if no API token is provided
if (is.null(api_token)) {
cat("No API token provided. Attempting to get a demo token...\n")
api_token <- get_demo_token()
cat("Using demo token:", substr(api_token, nchar(api_token)-7, nchar(api_token)), "...\n")
}
# Create example dataset
df <- create_example_dataset()
print("Created example dataset:")
print(head(df))
# Create segments
segments_df <- create_segments(df, "text", window_size = 3, api_token = api_token)
# Detect arguments
detect_df <- wiba_detect(df, api_token = api_token)
print("\nDetected arguments:")
print(head(detect_df))
# Extract topics
texts <- detect_df$text
extract_df <- wiba_extract(texts, api_token = api_token)
print("\nExtracted topics:")
print(head(extract_df))
# Analyze stance
topics <- extract_df$extracted_topic # Note: column name may be different based on API response
stance_df <- wiba_stance(texts, topics, api_token = api_token)
print("\nStance analysis:")
print(head(stance_df))
# Combine results (adjust column names as needed based on actual API responses)
result_df <- detect_df
result_df$extracted_topic <- extract_df$extracted_topic
result_df$stance_prediction <- stance_df$stance_prediction
result_df$stance_confidence <- stance_df$stance_confidence
print("\nCombined results:")
print(head(result_df))
# Return all results
return(list(
original_data = df,
segments = segments_df,
detection = detect_df,
topics = extract_df,
stance = stance_df,
combined = result_df
))
}
# Example of running the analysis
# Uncomment and run:
# API_TOKEN <- "your_api_token_here" # Replace with your actual token
# results <- main(API_TOKEN)
#
# # Or to use a demo token:
# results <- main()