-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
266 lines (234 loc) · 8.27 KB
/
app.R
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
library(shiny)
library(bslib)
library(glue)
library(httr)
library(jsonlite)
library(digest)
library(memoise)
library(typedjs)
OPENAI_API_KEY <- Sys.getenv("OPENAI_API_KEY")
GITHUB_API_KEY <- Sys.getenv("GITHUB_API_KEY")
# Memoized API functions
fetch_github_profile <- memoise(function(username) {
url <- paste0("https://api.github.com/users/", username)
headers <- add_headers(
"Accept" = "application/json",
"User-Agent" = "github-roast.pages.dev"
)
if (GITHUB_API_KEY != "") {
headers <- c(headers, add_headers("Authorization" = paste("token", GITHUB_API_KEY)))
}
response <- GET(url, headers)
if (response$status_code == 200) {
return(content(response, as = "parsed"))
} else {
stop("Failed to fetch GitHub profile.")
}
})
fetch_github_repos <- memoise(function(username) {
url <- paste0("https://api.github.com/users/", username, "/repos?sort=updated&per_page=10")
headers <- add_headers(
"Accept" = "application/json",
"User-Agent" = "github-roast.pages.dev"
)
response <- GET(url, headers)
if (response$status_code == 200) {
return(content(response, as = "parsed"))
} else {
stop("Failed to fetch GitHub repositories.")
}
})
fetch_readme <- memoise(function(username) {
readme_urls <- c(
paste0("https://raw.githubusercontent.com/", username, "/", username, "/main/README.md"),
paste0("https://raw.githubusercontent.com/", username, "/", username, "/master/README.md")
)
for (url in readme_urls) {
response <- GET(url)
if (response$status_code == 200) {
return(content(response, as = "text"))
}
}
return("")
})
# OpenAI API function
generate_roast <- function(prompt) {
response <- POST(
url = "https://api.openai.com/v1/chat/completions",
add_headers(Authorization = paste("Bearer", OPENAI_API_KEY)),
content_type_json(),
encode = "json",
body = list(
model = "gpt-4o-mini",
temperature = 1,
messages = list(
list(role = "system", content = ""),
list(role = "user", content = prompt)
)
)
)
content <- content(response, as = "parsed")
return(content$choices[[1]]$message$content)
}
# UI
ui <- page(
lang = "en",
includeCSS("www/style.css"),
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"),
tags$link(rel = "stylesheet", href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css")
),
fixedPanel(
bottom = 10, left = 10,
HTML('<script type="text/javascript" src="https://cdnjs.buymeacoffee.com/1.0.0/button.prod.min.js" data-name="bmc-button" data-slug="noamane" data-color="#FFDD00" data-emoji="" data-font="Cookie" data-text="Buy me a coffee" data-outline-color="#000000" data-font-color="#000000" data-coffee-color="#ffffff" ></script>')
),
fluidRow(
column(6,
div(style = "padding: 100px;",
card(
card_header(
card_title(
fluidRow(
tags$div(class = "flex-grow-1 mr-2",
tags$div(class = "input-group",
tags$div(class = "input-group-prepend",
tags$span(class = "input-group-text btn-light", style = "border-radius: 0.25rem 0 0 0.25rem;", "https://github.com/")
),
tags$input(type = "text", class = "form-control", id = "username", placeholder = "hadley")
)
)
),
br(),
fluidRow(
column(6, input_task_button(id = "roast", label = "🔥 Roast",style= 'width:100%;', class = "btn btn-light")),
column(6, input_task_button(id= "compliment", label = "❤️ Compliment," ,style= 'width:100%;', class = "btn btn-light"))
)
)
),
card_body(
fluidRow(
column(12, uiOutput("response"))
)
)
)
)
),
column(6, uiOutput("cardolo"))
)
)
# Server
server <- function(input, output, session) {
github_data <- reactiveVal(NULL)
# Function to fetch GitHub data
fetch_github_data <- function(username) {
tryCatch({
profile <- fetch_github_profile(username)
repos <- fetch_github_repos(username)
readme <- fetch_readme(username)
list(
profile = profile,
repos = repos,
readme = readme
)
}, error = function(e) {
showNotification(paste("Error fetching data:", e$message), type = "error")
NULL
})
}
# Generate GitHub roast or compliment
generate_github_roast <- function(username, roast = TRUE) {
data <- github_data()
profile_data <- list(
name = data$profile$name,
bio = data$profile$bio,
company = data$profile$company,
location = data$profile$location,
followers = data$profile$followers,
following = data$profile$following,
public_repos = data$profile$public_repos,
profile_readme = data$readme,
last_10_repositories = lapply(data$repos, function(repo) {
list(
name = repo$name,
description = repo$description,
language = repo$language,
stargazers_count = repo$stargazers_count,
open_issues_count = repo$open_issues_count,
license = repo$license,
fork = repo$fork
)
})
)
if (roast) {
prompt <- paste("Give a short and harsh roast with some emojis for the following GitHub profile:", username,
". Here are the details:", toJSON(profile_data))
} else {
prompt <- paste("Give a short thoughtful, motivating, and positive compliment with some emojis for the following GitHub profile:", username,
". Highlight their strengths and achievements. Here are the details:", toJSON(profile_data))
}
generate_roast(prompt)
}
# Function to handle button clicks
handle_button_click <- function(roast = TRUE) {
req(input$username)
username <- input$username
# Fetch GitHub data
data <- fetch_github_data(username)
if (is.null(data)) return()
github_data(data)
# Generate and display roast/compliment
result <- generate_github_roast(username, roast)
output$response <- renderUI({
typedjs::typed(result)
})
# Update GitHub card
output$cardolo <- renderUI({
req(github_data())
data <- github_data()
div(
id = "app",
tags$section(
class = "ticket",
tags$header(
class = "front",
tags$div(class = "holo"),
tags$img(class = "logo", src = "https://www.svgrepo.com/show/353783/github-octocat.svg"),
tags$aside(class = "divider")
),
tags$section(
class = "back",
tags$div(class = "holo"),
tags$div(
class = "data",
tags$a(href = 'https://github.com/anuraghazra/github-readme-stats',
tags$img(width = "100%", align = "center", src = glue("https://github-readme-stats.vercel.app/api?username={username}&show_icons=true&theme=transparent"))
),
br(),
tags$a(href = 'https://github.com/anuraghazra/github-readme-stats',
tags$img(width = "100%", align = "center", src = glue("https://github-readme-stats.vercel.app/api/top-langs/?username={username}&layout=compact&theme=transparent"))
)
),
tags$aside(
class = "divider",
tags$div(
tags$img(id = "avatar", src = data$profile$avatar_url, alt = ""),
tags$h3(data$profile$name)
)
)
)
),
includeScript("www/cust.js")
)
})
}
# Roast button
observeEvent(input$roast, {
handle_button_click(roast = TRUE)
})
# Compliment button
observeEvent(input$compliment, {
handle_button_click(roast = FALSE)
})
}
# Run the application
shiny::shinyApp(ui, server,options = list(port =3838 ,host = "0.0.0.0"))