File size: 8,015 Bytes
f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 1f88e84 f15e0a9 1f88e84 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 f15e0a9 cec6b19 |
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 |
"""
This code adapts the following code for Linear.app. It is not official code.
https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/_webhooks_server.py
https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/_webhooks_payload.py
Currently, it only supports a small subset of the Issue Object.
https://studio.apollographql.com/public/Linear-API/variant/current/schema/reference/objects/Issue
You need to set `api_key = linear-api-key` and `webhook_secret = linear-webhook-secret` in your `.env` file.
Since the local URL changes with each startup, the URL is updated in the Linear API at startup.
At startup, it overwrites the webhook with the label specified by `target_webhook_label` (default value: Gradio).
You need to pre-install gradio, fastapi, and pydantic.
You need to describe your Linear API key and Linear webhook secret in the `.env` file.
Also, since this example is only for Update, please create a Webhook with the label Gradio beforehand.
** Copyright Notice for Linear.app Adaptation **
# Copyright 2025-present, Akihito Miyazaki
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
** License Notice for Hugging Face Hub Library **
# Copyright 2023-present, the HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
This code includes parts of the Hugging Face Hub library, which is licensed under the Apache License, Version 2.0.
The full text of the license can be found at: http://www.apache.org/licenses/LICENSE-2.0
"""
import os
from pprint import pprint
import gradio as gr
from smolagents import CodeAgent, HfApiModel
from linear_api_utils import execute_query
from gradio_webhook_server import WebhooksServer
from gradio_webhook_payload import WebhookPayload
from sleep_per_last_token_model import SleepPerLastTokenModelLiteLLM
# .env
"""
LINEAR_API_KEY="lin_api_***"
HF_TOKEN = "hf_***"
LINEAR_WEBHOOK_KEY="lin_wh_***"
GROQ_API_KEY = "gsk_***"
"""
def get_env_value(key, is_value_error_on_null=True):
value = os.getenv(key)
if value is None:
from dotenv import load_dotenv
load_dotenv()
value = os.getenv(key)
if is_value_error_on_null and value is None:
raise ValueError(f"Need {key} on secret or .env(If running on local)")
return value
# SETTINGS
LINEAR_ISSUE_LABEL = "huggingface-public" # only show issue with this label,I added for demo you can remove this
LINEAR_WEBHOOK_LABEL = "Huggingface" # you have to create in linear before run script
# set secret key on Space setting or .env(local)
# hf_token = get_env_value("HF_TOKEN")
groq_api_key = get_env_value("GROQ_API_KEY")
api_key = get_env_value("LINEAR_API_KEY")
webhook_key = get_env_value("LINEAR_WEBHOOK_KEY")
if api_key is None:
raise ValueError("Need LINEAR_API_KEY on secret")
if webhook_key is None:
raise ValueError("Need LINEAR_WEBHOOK_KEY on secret")
webhook_query_text = """
query {
webhooks{
nodes {
id
label
url
}
}
}
"""
target_webhook_label = LINEAR_WEBHOOK_LABEL # filter not working,set manual
target_webhook_id = None
result = execute_query("webhook", webhook_query_text, api_key)
for webhook in result["data"]["webhooks"]["nodes"]:
if target_webhook_label == webhook["label"]:
target_webhook_id = webhook["id"]
app = None
"""
model = HfApiModel(
max_tokens=100,
temperature=0.5,
model_id="google/gemma-2-2b-it",
custom_role_conversions=None,
token=hf_token,
)
"""
def update():
# result = agent.run(f"how to solve this issue:{app.text}")
# return app.text, result
return "", ""
# still testing file or memory
def load_text(path):
with open(path, "r") as f:
return f.read()
def save_text(path, text):
with open(path, "w") as f:
f.write(text)
def update_text():
return app.issue, app.output
with gr.Blocks() as demo:
gr.HTML("""<h1>Linear.app Webhook Server</h1>
<p>This is Demo of Direct Webhook-triggered AIAgen</p>
<p>it's still just simple code,you have to reload when webhooked.</p>
<p><b>Imagine an agent, responding instantly.</b></p>
<p>Technically Gradio have no way to update without action<p>
<p></p><br>
<p>I'm confused by Hugging Face's new pricing system. I'm worried about potentially massive inference API bills, so I switched to Groq.</p>
<p>I believe my use of the Groq API is currently compliant with Hugging Face's Content Policy.</p>
<p>If you have any questions, please disable the Space or contact me before taking any action against my account. Thank you for your understanding.</p>
""")
with gr.Row():
with gr.Column():
gr.Markdown("## Issue")
# issue = gr.Markdown(load_text("issue.md"))
issue = gr.Markdown("issue")
with gr.Column():
gr.Markdown("## Agent advice(Don't trust them completely)")
# output = gr.Markdown(load_text("output.md"))
output = gr.Markdown("agent result")
demo.load(update_text, inputs=None, outputs=[issue, output])
# bt = gr.Button("Ask AI")
# bt.click(update, outputs=[issue_box, output_box])
app = WebhooksServer(
ui=demo,
webhook_secret=webhook_key, # loaded by load_api_key
)
app.output = "join course"
app.issue = "how to learn smolagent"
@app.add_webhook("/linear_webhook")
async def updated(payload: WebhookPayload):
def generate_agent():
model = SleepPerLastTokenModelLiteLLM(
max_tokens=250,
temperature=0.5,
model_id="groq/llama3-8b-8192",
api_base="https://api.groq.com/openai/v1/",
api_key=groq_api_key,
)
agent = CodeAgent(
model=model,
tools=[], ## add your tools here (don't remove final answer)
max_steps=1,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
)
return agent
pprint(payload.dict(), indent=4)
data = payload.dict()["data"]
has_label = True
if LINEAR_ISSUE_LABEL:
has_label = False
for label in data["labels"]:
if label["name"] == LINEAR_ISSUE_LABEL:
has_label = True
if has_label:
text = data["description"]
app.issue = text
# save_text("issue.md", text)
agent = generate_agent()
result = agent.run(f"how to solve this issue:{text}")
app.output = result
# save_text("output.md", result)
return {"message": "ok"}
def webhook_update(url):
webhook_update_text = """
mutation {
webhookUpdate(
id: "%s"
input:{
url:"%s"
}
) {
success
}
}
""" % (target_webhook_id, url)
result = execute_query("webhook_update", webhook_update_text, api_key)
if __name__ == "__main__": # without main call twice
app.launch(webhook_update=webhook_update)
|