Spaces:
Sleeping
Sleeping
init
Browse files
app.py
CHANGED
@@ -1,3 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
|
3 |
import gradio as gr
|
@@ -6,15 +20,28 @@ from smolagents import CodeAgent, tool
|
|
6 |
from linear_api_utils import execute_query
|
7 |
from sleep_per_last_token_model import SleepPerLastTokenModelLiteLLM
|
8 |
|
9 |
-
# .env
|
10 |
"""
|
11 |
LINEAR_API_KEY="lin_api_***"
|
12 |
-
HF_TOKEN = "hf_***"
|
13 |
GROQ_API_KEY = "gsk_***"
|
|
|
14 |
"""
|
15 |
|
16 |
|
17 |
def get_env_value(key, is_value_error_on_null=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
value = os.getenv(key)
|
19 |
if value is None:
|
20 |
from dotenv import load_dotenv
|
@@ -44,6 +71,19 @@ model_id = "groq/llama3-8b-8192"
|
|
44 |
|
45 |
|
46 |
def add_comment(issue_id, model_name, comment):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
comment = comment.replace('"', '\\"').replace("\n", "\\n") # escape doublequote
|
48 |
# header = f"<!---\\n start-ai-comment({model_name}) \\n--->\\n"
|
49 |
header = f"[ ](start-ai-comment:{model_name})\\n"
|
@@ -71,6 +111,12 @@ issue_id = None
|
|
71 |
|
72 |
|
73 |
def change_state_reviewing():
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
get_state_query_text = """
|
75 |
query Sate{
|
76 |
workflowStates(filter:{team:{id:{eq:"%s"}}}){
|
@@ -161,6 +207,12 @@ def get_todo_issue() -> str:
|
|
161 |
|
162 |
|
163 |
def generate_agent():
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
model = SleepPerLastTokenModelLiteLLM(
|
165 |
max_tokens=250,
|
166 |
temperature=0.5,
|
@@ -185,6 +237,15 @@ team_id = None
|
|
185 |
|
186 |
|
187 |
def update_text():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
def get_team_id(team_name):
|
189 |
teams_text = """
|
190 |
query Teams {
|
@@ -221,18 +282,23 @@ Finally, return the result of solving the Todo.
|
|
221 |
"""
|
222 |
)
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
#
|
227 |
|
228 |
return issue_text, agent_text
|
229 |
|
230 |
|
231 |
with gr.Blocks() as demo:
|
232 |
-
gr.HTML("""
|
233 |
-
<
|
|
|
|
|
|
|
|
|
234 |
<p>Need Linear.app acount and api key</a>
|
235 |
-
<p>
|
|
|
236 |
""")
|
237 |
with gr.Row():
|
238 |
with gr.Column():
|
@@ -245,10 +311,10 @@ with gr.Blocks() as demo:
|
|
245 |
output = gr.Markdown("agent result")
|
246 |
demo.load(update_text, inputs=None, outputs=[issue, output])
|
247 |
|
248 |
-
#
|
249 |
# bt = gr.Button("Next Todo")
|
250 |
# bt.click(update_text, inputs=None, outputs=[issue, output])
|
251 |
|
252 |
|
253 |
-
if __name__ == "__main__": # without main call twice
|
254 |
demo.launch()
|
|
|
1 |
+
# Copyright 2025 Akihito Miyazaki. team. All rights reserved.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
|
15 |
import os
|
16 |
|
17 |
import gradio as gr
|
|
|
20 |
from linear_api_utils import execute_query
|
21 |
from sleep_per_last_token_model import SleepPerLastTokenModelLiteLLM
|
22 |
|
23 |
+
# if use .env need these lines HF_TOKEN is optional
|
24 |
"""
|
25 |
LINEAR_API_KEY="lin_api_***"
|
|
|
26 |
GROQ_API_KEY = "gsk_***"
|
27 |
+
HF_TOKEN = "hf_***"
|
28 |
"""
|
29 |
|
30 |
|
31 |
def get_env_value(key, is_value_error_on_null=True):
|
32 |
+
"""
|
33 |
+
Gets an environment variable's value, loading from .env if needed.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
key (str): Environment variable name.
|
37 |
+
is_value_error_on_null (bool): Raise ValueError if not found (default: True).
|
38 |
+
|
39 |
+
Returns:
|
40 |
+
str: Environment variable value.
|
41 |
+
|
42 |
+
Raises:
|
43 |
+
ValueError: If `key` is not found and `is_value_error_on_null` is True.
|
44 |
+
"""
|
45 |
value = os.getenv(key)
|
46 |
if value is None:
|
47 |
from dotenv import load_dotenv
|
|
|
71 |
|
72 |
|
73 |
def add_comment(issue_id, model_name, comment):
|
74 |
+
"""
|
75 |
+
Add comment to an issue.
|
76 |
+
|
77 |
+
|
78 |
+
Args:
|
79 |
+
issue_id (str): Issue ID.
|
80 |
+
model_name (str): Model name added as title.
|
81 |
+
comment (str): Comment text.
|
82 |
+
|
83 |
+
|
84 |
+
Returns:
|
85 |
+
str: query result json.
|
86 |
+
"""
|
87 |
comment = comment.replace('"', '\\"').replace("\n", "\\n") # escape doublequote
|
88 |
# header = f"<!---\\n start-ai-comment({model_name}) \\n--->\\n"
|
89 |
header = f"[ ](start-ai-comment:{model_name})\\n"
|
|
|
111 |
|
112 |
|
113 |
def change_state_reviewing():
|
114 |
+
"""
|
115 |
+
Change the state of an issue to "Reviewing".
|
116 |
+
|
117 |
+
Returns:
|
118 |
+
None
|
119 |
+
"""
|
120 |
get_state_query_text = """
|
121 |
query Sate{
|
122 |
workflowStates(filter:{team:{id:{eq:"%s"}}}){
|
|
|
207 |
|
208 |
|
209 |
def generate_agent():
|
210 |
+
"""
|
211 |
+
Generate an agent.
|
212 |
+
|
213 |
+
Returns:
|
214 |
+
An agent.
|
215 |
+
"""
|
216 |
model = SleepPerLastTokenModelLiteLLM(
|
217 |
max_tokens=250,
|
218 |
temperature=0.5,
|
|
|
237 |
|
238 |
|
239 |
def update_text():
|
240 |
+
"""
|
241 |
+
Get the Todo issue and generate an agent.
|
242 |
+
agent solve the issue and return text to Gradio outputs
|
243 |
+
|
244 |
+
Returns:
|
245 |
+
A string describing the current issue.
|
246 |
+
A string describing the agent advice.
|
247 |
+
"""
|
248 |
+
|
249 |
def get_team_id(team_name):
|
250 |
teams_text = """
|
251 |
query Teams {
|
|
|
282 |
"""
|
283 |
)
|
284 |
|
285 |
+
# If you duplicate space uncomment below
|
286 |
+
# add_comment(issue_id, model_id, agent_text)
|
287 |
+
# change_state_reviewing()
|
288 |
|
289 |
return issue_text, agent_text
|
290 |
|
291 |
|
292 |
with gr.Blocks() as demo:
|
293 |
+
gr.HTML("""
|
294 |
+
<h1>Initial API-Based Smolagents and Linear.app Integration Example</h1>
|
295 |
+
<p>Large language models, like 70B parameter models, can often readily utilize tools such as <code>add_comment</code> or <code>change_state</code>, potentially handling multiple issues concurrently.</p>
|
296 |
+
<p>However, smaller models may require repeated calls to a tool or even fail to utilize it entirely.</p>
|
297 |
+
<p>Therefore, this initial example focuses on the <code>get_todo_issue()</code> tool.</p>
|
298 |
+
<h2>Post-Duplication/Cloning Instructions</h2>
|
299 |
<p>Need Linear.app acount and api key</a>
|
300 |
+
<p>change script team name to your team name,add "Reviewing" State in your linear.app team setting<p>
|
301 |
+
<p>comment out add_comment(),change_state_reviewing()</p>
|
302 |
""")
|
303 |
with gr.Row():
|
304 |
with gr.Column():
|
|
|
311 |
output = gr.Markdown("agent result")
|
312 |
demo.load(update_text, inputs=None, outputs=[issue, output])
|
313 |
|
314 |
+
# for manual solve
|
315 |
# bt = gr.Button("Next Todo")
|
316 |
# bt.click(update_text, inputs=None, outputs=[issue, output])
|
317 |
|
318 |
|
319 |
+
if __name__ == "__main__": # without main call demo called twice
|
320 |
demo.launch()
|