import os from groq import Groq import gradio as gr import aiofiles client = Groq(api_key=os.environ.get("GROQ_API_KEY")) system_prompt = { "role": "system", "content": "You are a useful assistant that generates and refines SVG diagrams. Ensure the SVG code is valid and starts with ." } previous_svg = "" async def generate_diagram_llama(description, option, existing_svg=None): global previous_svg messages = [system_prompt] if option == "Refinar anterior" and previous_svg: messages.append({"role": "user", "content": f"Here is the existing SVG diagram: {previous_svg}"}) messages.append({"role": "user", "content": f"Refine the SVG diagram based on the following description: {description}"}) elif option == "Refinar existente" and existing_svg: messages.append({"role": "user", "content": f"Here is the existing SVG diagram: {existing_svg}"}) messages.append({"role": "user", "content": f"Refine the SVG diagram based on the following description: {description}"}) else: messages.append({"role": "user", "content": f"Generate an SVG diagram based on the following description: {description}"}) response_content = '' stream = client.chat.completions.create( model="llama3-70b-8192", messages=messages, max_tokens=1024, temperature=1.3, stream=True ) for chunk in stream: content = chunk.choices[0].delta.content if content: response_content += chunk.choices[0].delta.content print("Resposta da API:", response_content) if "```" in response_content: svg_content = response_content.split("```")[1].strip() else: svg_content = response_content.strip() if not svg_content.startswith("