Spaces:
Sleeping
Sleeping
module MyController | |
using Genie | |
using Genie.Renderer.Html | |
using Colors | |
using OrderedCollections | |
using HTTP, JSON, Statistics | |
## HOW THIS WORKS: | |
## 1. Call the API to get the fact-checks for each chunk of text | |
## 2. Generate a gradient text based on the scores from the API | |
## 3. Return the gradient text | |
## Call API to process chunks of text | |
""" | |
## Function to call this endpoint: https://stefanjwojcik-misinfo-detection-app.hf.space/fastfactsearch?claim=climate%20change%20is%20a%20hoax&model=factchecks&top_k=5 | |
Example: | |
oc_api_call("climate change is a hoax") | |
""" | |
function oc_api_call(text::String) | |
text = HTTP.escapeuri(text) | |
url = "https://stefanjwojcik-misinfo-detection-app.hf.space/fastfactsearch?claim=$(text)&model=factchecks&top_k=5" | |
response = HTTP.get(url) | |
return JSON.parse(String(response.body)) | |
end | |
# This function will call the API, and generate scores for each chunk in the transcript | |
## Inaug link: WVOvmHUu8Vw | |
function generate_factchecks(transcript::Vector{String}) | |
# Create a dictionary with the text and fact-check URLs | |
factchecks = Dict[] | |
for line in transcript | |
api_output = oc_api_call(line) | |
newdict = Dict("text" => line, "score" => api_output[1]["score"], "claimUrl" => api_output[1]["claimUrl"]) | |
push!(factchecks, newdict) | |
end | |
return factchecks | |
end | |
function generate_url_list(factchecks::Vector{Dict}, threshold=0.5) | |
urls = [factcheck["claimUrl"] for factcheck in factchecks if factcheck["score"] > threshold] | |
url_list = "<ol>" | |
for (i, url) in enumerate(urls) | |
url_list *= "<li><a href=\"$(url)\" target=\"_blank\">$(url)</a></li>" | |
end | |
url_list *= "</ol>" | |
return url_list | |
end | |
function scores_to_colors(scores::Vector{T}, midpoint_threshold=.5) where T <: Real | |
# Create a color gradient from red to blue | |
c1 = colorant"blue" #true color | |
c2 = colorant"red" #false color | |
midpoint = mean(extrema(scores)) | |
if midpoint < midpoint_threshold # rough heuristic of true/false distro | |
padded_colors = hex.(range(c1, stop=c2, length=length(scores)*2)) # expands the range | |
colors = padded_colors[1:length(scores)] #take only the first half of the range | |
else | |
colors = hex.(range(c1, stop=c2, length=length(unique(scores)))) | |
end | |
colors = ["#" * color for color in colors] | |
# Map scores to colors | |
return Dict(zip(unique(sort(scores)), colors)) | |
end | |
function gradient_text(factchecks::Vector{Dict}, threshold::Float64=0.6) | |
# Create colordict from scores | |
allscores = [factcheck["score"] for factcheck in factchecks] | |
colordict = scores_to_colors(allscores) | |
# Create span elements for each character with gradient colors | |
spans = [] | |
n_refs = 0 | |
for factcheck in factchecks | |
# Get the score for the current character | |
score = factcheck["score"] | |
color = colordict[score] | |
textval = factcheck["text"] | |
if score > threshold | |
n_refs += 1 | |
textval = textval * " ($n_refs)" | |
end | |
# Create span with specific color | |
push!(spans, "<span style=\"color: $(color);\">$(textval)</span>") | |
end | |
# Join all spans | |
gradient_text = join(spans, " ") | |
# Generate URL list | |
url_list = generate_url_list(factchecks, threshold) | |
# Append URL list to gradient text | |
return gradient_text * "<br><br>" * url_list | |
end | |
function youtubeUrlToId(url::String) | |
if occursin("youtube.com", url) | |
video_id = match(r"(?<=v=)[\w-]+", url).match | |
elseif occursin("youtu.be", url) | |
video_id = match(r"(?<=be/)[\w-]+", url).match | |
else | |
video_id = url | |
end | |
return string(video_id) | |
end | |
end # end module |