READMEs are essential for clear communication and undertaking understanding, however creating them could be a time-consuming chore. On this weblog put up, we’ll discover how we are able to leverages Lyzr-Automata’s capabilities to remodel a file of code into clear, concise, and informative READMEs.
Create a folder, arrange a digital surroundings and activate it. Create .env
file along with your OPENAI_API_KEY. Then set up the next libraries to get began.
streamlit
: for constructing the net app interface.lyzr_automata
: for implementing our AI fashions, and duties.dotenv
: for loading surroundings variables (API key).
git+https://github.com/LyzrCore/lyzr-automata.git@principal
streamlit==1.33.0
python-dotenv==1.0.1
1.Import Libraries
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Job
from lyzr_automata.duties.task_literals import InputType, OutputTypefrom dotenv import load_dotenv
import os
load_dotenv()
# Load OpenAI API Key
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
2. generate_readme_file Perform
OpenAIModel
— Create our fashions utilizing OpenAI Key and specify the mannequin kind and identify. (GPT-4)Agent
— readme_agent, create a Lyzr Agent for readme era.Job
— readme_creator_task, create Lyzr Job with directions and enter to generate readme file.
# Perform to generate readme file
def generate_readme_file(file_contents, output_headers):
# Initialize OpenAI Textual content Mannequin
open_ai_model_text = OpenAIModel(
api_key=OPENAI_API_KEY,
parameters={
"mannequin": "gpt-4-turbo-preview",
"temperature": 0.2,
"max_tokens": 4000,
},
)# Create a markdown generator Agent
readme_agent = Agent(
prompt_persona="You're an clever agent that may write in markdown format, however don't embrace ```markdown within the output.",
function="Readme Generator"
)
# Create a markdown generator Job
readme_creator_task = Job(
identify="Generate Readme Job",
agent=readme_agent,
output_type=OutputType.TEXT,
input_type=InputType.TEXT,
mannequin=open_ai_model_text,
directions="Write a README file for the supplied code. Return solely the output. Headers to be included are: " + output_headers,
log_output=True,
enhance_prompt=False,
default_input=file_contents,
).execute()
with open("generated_file.md", "w") as my_file:
my_file.write(readme_creator_task)
return readme_creator_task
3. Enter Elements
st.file_uploader
— To add code filest.text_area
— Textual content space to enter headers requiredst.button
— Button to submit inputs
# File uploader enter
code_file = st.file_uploader("Add your .py file")# Headers enter
output_headers = st.text_area("Headers to incorporate", '''1. Title
2. Overview
3. Dependencies
4. Circulation of the code in a bulleted format with 1-2 sentences every level.
5. The best way to run
''')
# Submit button
submit_file = st.button("Submit")
if submit_file:
file_contents = code_file.learn() # Learn file
readme_content = generate_readme_file(file_contents, output_headers) # Generate Readme file
st.write(readme_content) # Print output
st.download_button('Obtain readme', readme_content, file_name="generated_readme.md", mime="textual content/markdown") # Obtain output
streamlit run principal.py