raseel-zymr commited on
Commit
cd3f670
·
1 Parent(s): b105b9f

Generate diagram based on prompt

Browse files
Files changed (2) hide show
  1. app.py +55 -0
  2. virtualenv_mgr.py +43 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import time
4
+ # import tempfile
5
+ from virtualenv_mgr import VirtualenvManager
6
+
7
+ import streamlit as st
8
+ from langchain.llms import OpenAI
9
+ from langchain.prompts import PromptTemplate
10
+ from langchain.chains import LLMChain, SequentialChain
11
+ from langchain.memory import ConversationBufferMemory
12
+
13
+ os.environ['OPENAI_API_KEY'] = st.secrets["openai_api_key"]
14
+
15
+ st.title("dAIgramGen : The AI-based Diagram Generator")
16
+
17
+ daig_name = st.text_input("Diagram name")
18
+ daig_name_lower = daig_name.lower()
19
+ daig_name_underscore = daig_name_lower.replace(" ", "_")
20
+ daig_python_filename = daig_name_underscore + ".py"
21
+ daig_png_filename = daig_name_underscore + ".png"
22
+
23
+ daig_prompt = st.text_area("Prompt for diagrams")
24
+
25
+ daig_template = PromptTemplate(
26
+ input_variables = ['topic', 'name'],
27
+ template = 'Using the Python diagrams library generate the code for {topic} and name the diagram {name}. Just return the code. No explaination of the code needed'
28
+ )
29
+
30
+ daig_memory = ConversationBufferMemory(input_key='topic', memory_key='daig_history')
31
+
32
+ llm = OpenAI(temperature=0.9)
33
+ daig_chain = LLMChain(llm=llm, prompt=daig_template, verbose=True, output_key='code', memory=daig_memory)
34
+
35
+ if daig_prompt and daig_name:
36
+ code = daig_chain.run({'topic':daig_prompt, 'name': daig_name})
37
+ st.subheader('Generated Code')
38
+ st.code(code)
39
+ venv = VirtualenvManager()
40
+ venv.create_env()
41
+ venv.add_dependency("diagrams")
42
+
43
+ if os.path.exists(daig_python_filename):
44
+ os.remove(daig_python_filename)
45
+ f = open(daig_python_filename, "w")
46
+ f.write(code)
47
+ f.flush()
48
+ process = venv.run_code(f.name)
49
+
50
+ time.sleep(5)
51
+ if os.path.exists(daig_png_filename):
52
+ st.image(daig_png_filename)
53
+
54
+ with st.expander('Code History'):
55
+ st.info(daig_memory.buffer)
virtualenv_mgr.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import string
3
+ import random
4
+ import os
5
+ import subprocess
6
+ from virtualenv import cli_run
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+ RANDOM_NAME_LENGTH = 16
11
+
12
+
13
+ class VirtualenvManager:
14
+ def __init__(self, name: str = "", base_path="/tmp") -> None:
15
+ if not name:
16
+ name = ""
17
+ for _ in range(RANDOM_NAME_LENGTH):
18
+ population = string.ascii_letters + string.digits
19
+ char = random.sample(population, k=1)
20
+ name += char[0]
21
+ self.name = name
22
+ self.path = os.path.join(base_path, name)
23
+ self.python_interpreter = os.path.join(self.path, "bin/python3")
24
+ self.dependencies = []
25
+
26
+ def add_dependency(self, dependency):
27
+ logger.info("Adding dependency '%s' ", dependency)
28
+ self.dependencies.append(dependency)
29
+
30
+ def create_env(self):
31
+ logger.info("Creating virtualenv at path '%s' ", self.path)
32
+ cli_run([self.path], setup_logging=False)
33
+
34
+ def run_code(self, fname):
35
+ logger.info("Installing dependencies")
36
+ process = subprocess.run(
37
+ [self.python_interpreter, "-m", "pip", "install"] + self.dependencies,
38
+ capture_output=True,
39
+ )
40
+ process = subprocess.run(
41
+ [self.python_interpreter, fname], capture_output=True,
42
+ )
43
+ return process