rmayormartins commited on
Commit
154a404
1 Parent(s): 31934d3

Add application file

Browse files
Files changed (3) hide show
  1. README.md +30 -7
  2. app.py +93 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,36 @@
1
  ---
2
- title: Svgbuilder Llama3
3
- emoji: 🌖
4
- colorFrom: gray
5
- colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 4.38.1
8
  app_file: app.py
9
  pinned: false
10
- license: ecl-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: svgbuilder-llama3
3
+ emoji: 🦙🤖🔂
4
+ colorFrom: blue
5
+ colorTo: red
6
  sdk: gradio
7
+ sdk_version: "4.12.0"
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ # About
13
+
14
+ This project allows users to generate and refine SVG diagrams using descriptions. The system utilizes the Groq cloud API and Llama3-70b-8192 model to create and improve SVG content based on user inputs.
15
+
16
+ # Tech
17
+
18
+ This project uses:
19
+ - Groq cloud API
20
+ - Llama3-70b-8192
21
+ - gradio
22
+ - svgwrite
23
+ - aiofiles
24
+
25
+ ## More information
26
+
27
+ Ramon Mayor Martins (2024)
28
+ - Email: rmayormartins@gmail.com
29
+ - Website: https://rmayormartins.github.io/
30
+ - Twitter: @rmayormartins
31
+ - GitHub: https://github.com/rmayormartins
32
+
33
+ ## Special thanks
34
+ - Prof. Dr. Roberto Wanderley da Nóbrega
35
+ - Meta: https://llama.meta.com/llama3/
36
+ - Groq: https://groq.com/
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from groq import Groq
3
+ import gradio as gr
4
+ import aiofiles
5
+
6
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
+
8
+ system_prompt = {
9
+ "role": "system",
10
+ "content": "You are a useful assistant that generates and refines SVG diagrams. Ensure the SVG code is valid and starts with <svg>."
11
+ }
12
+
13
+ previous_svg = ""
14
+
15
+ async def generate_diagram_llama(description, option, existing_svg=None):
16
+ global previous_svg
17
+ messages = [system_prompt]
18
+
19
+ if option == "Refinar anterior" and previous_svg:
20
+ messages.append({"role": "user", "content": f"Here is the existing SVG diagram: {previous_svg}"})
21
+ messages.append({"role": "user", "content": f"Refine the SVG diagram based on the following description: {description}"})
22
+ elif option == "Refinar existente" and existing_svg:
23
+ messages.append({"role": "user", "content": f"Here is the existing SVG diagram: {existing_svg}"})
24
+ messages.append({"role": "user", "content": f"Refine the SVG diagram based on the following description: {description}"})
25
+ else:
26
+ messages.append({"role": "user", "content": f"Generate an SVG diagram based on the following description: {description}"})
27
+
28
+ response_content = ''
29
+ stream = client.chat.completions.create(
30
+ model="llama3-70b-8192",
31
+ messages=messages,
32
+ max_tokens=1024,
33
+ temperature=1.3,
34
+ stream=True
35
+ )
36
+
37
+ for chunk in stream:
38
+ content = chunk.choices[0].delta.content
39
+ if content:
40
+ response_content += chunk.choices[0].delta.content
41
+
42
+ print("Resposta da API:", response_content)
43
+
44
+ if "```" in response_content:
45
+ svg_content = response_content.split("```")[1].strip()
46
+ else:
47
+ svg_content = response_content.strip()
48
+
49
+ if not svg_content.startswith("<svg"):
50
+ svg_content = "<svg width='100' height='100' xmlns='http://www.w3.org/2000/svg'><text x='10' y='20'>Invalid SVG</text></svg>"
51
+
52
+ previous_svg = svg_content
53
+ print("SVG Anterior Armazenado:", previous_svg)
54
+ return svg_content
55
+
56
+ async def create_svg_file(svg_code):
57
+ filename = "generated_diagram.svg"
58
+ async with aiofiles.open(filename, "w") as file:
59
+ await file.write(svg_code)
60
+ return filename
61
+
62
+ async def generate_and_display_diagram(description, option, existing_svg=None):
63
+ svg_code = await generate_diagram_llama(description, option, existing_svg)
64
+ svg_file = await create_svg_file(svg_code)
65
+ return svg_code, svg_file
66
+
67
+ with gr.Blocks(theme=gr.themes.Monochrome(), fill_height=True) as demo:
68
+ description_input = gr.Textbox(label="Description")
69
+ option_input = gr.Radio(choices=["Gerar novo", "Refinar anterior", "Refinar existente"], label="Opção")
70
+ existing_svg_input = gr.File(label="Upload Existing SVG (Optional)", visible=False)
71
+ svg_display = gr.HTML()
72
+ output_file = gr.File(label="Generated SVG")
73
+ submit_button = gr.Button("Generate")
74
+
75
+ async def update_output(description, option, existing_svg_file):
76
+ existing_svg = None
77
+ if option == "Refinar existente" and existing_svg_file:
78
+ async with aiofiles.open(existing_svg_file.name, "r") as file:
79
+ existing_svg = await file.read()
80
+ svg_code, svg_file = await generate_and_display_diagram(description, option, existing_svg)
81
+ return svg_code, svg_file
82
+
83
+ def toggle_file_input(option):
84
+ if option == "Refinar existente":
85
+ return gr.update(visible=True)
86
+ else:
87
+ return gr.update(visible=False)
88
+
89
+ option_input.change(toggle_file_input, inputs=option_input, outputs=existing_svg_input)
90
+ submit_button.click(update_output, inputs=[description_input, option_input, existing_svg_input], outputs=[svg_display, output_file])
91
+
92
+ demo.queue()
93
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ groq
2
+ gradio==4.12.0
3
+ svgwrite
4
+ aiofiles