File size: 3,367 Bytes
9b5b26a
 
 
 
c19d193
6aae614
cb0e56b
8fe992b
9b5b26a
f891d40
9b5b26a
f6970e9
 
 
 
5ce61c0
 
a68788a
55247ca
31654e0
 
965e596
 
 
 
 
 
 
 
 
 
 
 
 
 
d1de89b
965e596
7ffdc1a
965e596
 
31654e0
965e596
 
 
31654e0
965e596
31654e0
a79b233
9b5b26a
b08ea08
d40e476
9b5b26a
d40e476
9b5b26a
d40e476
3ee1c90
f04ae1b
5ce61c0
9b5b26a
6aae614
ae7a494
 
 
 
e121372
bf6d34c
 
8c58ee3
 
fe328e0
13d500a
8c01ffb
9b5b26a
 
8c01ffb
861422e
 
9b5b26a
8c01ffb
8fe992b
e1b4381
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
f6f8277
d40e476
 
 
 
 
5ce61c0
d40e476
4d290e7
d40e476
b22f1ba
 
 
965e596
 
2936384
 
 
 
965e596
d40e476
 
2112a80
62de5f2
12e231c
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
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from json_repair import repair_json

from Gradio_UI import GradioUI
import gradio as gr

import re
from markdownify import markdownify
from requests.exceptions import RequestException

with open('styles.css', 'r', encoding='utf-8') as file:
    styles = file.read()

with open('scripts.js', 'r', encoding='utf-8') as file:
    scripts = """
        async () => {
        function waitForElementById(id, callback) {
            const element = document.getElementById(id);
            if (element) {
                callback(element);
                return;
            }
            const observer = new MutationObserver((mutationsList, observer) => {
                for (let mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                const element = document.getElementById(id);
                        if (element) {
                            callback(element);
                            observer.disconnect();
                            return;
                        }
                    }
                }
            });
                observer.observe(document.body, { childList: true, subtree: true });
            }
            waitForElementById('root', () => {
                %s
            });
            
            }
        """ % (file.read())

@tool
def json_string_to_poster(json_string:str)-> str: 
    """A tool that generate a poster from stringified json object 
    Args:
        json_string: stringified json data
    """

    return f"""
        <script id="cheatsheets_json">{repair_json(json_string)}</script>
    """

final_answer = FinalAnswerTool()

# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 

model = HfApiModel(
max_tokens=2096,
temperature=0.5,
#model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)

# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer, DuckDuckGoSearchTool(), json_string_to_poster],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

def get_and_visualize_data(query):
    response = agent.run(query)
    return response
    

# 4. Создаем Gradio интерфейс
with gr.Blocks(css=styles) as demo:
    with gr.Row():
        prompt_input = gr.Textbox(label="prompt", placeholder="Example: create poster with npm commands")
    
    poster_display = gr.HTML("""
        <div id="root"></div>
    """);

    json_html = gr.HTML();

    prompt_input.submit(
        get_and_visualize_data,
        prompt_input,
        json_html,
    )

    demo.load(None, None, None, js=scripts)

demo.launch()