Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmurdza committed Dec 17, 2023
1 parent e93155b commit b8339f6
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from codechain.generation import ModifyCodeChain
from langchain.chat_models import ChatOpenAI

bash_to_python_prompt = '''\
bash_to_python_prompt = """\
convert this block of code as concisely as possible to
```python
import requests
Expand All @@ -19,32 +19,31 @@
use os.environ to get API keys
don't use intermediate variables
always use double quotes
'''

# Function to replace bash code blocks with python code blocks
def replace_bash_with_python(match):
bash_code = match.group(1)

# Call OpenAI API to translate Bash to Python
generator = ModifyCodeChain.from_instruction(
bash_to_python_prompt,
ChatOpenAI(model="gpt-3.5-turbo", temperature=0.2)
"""

prompts = {"python": bash_to_python_prompt}

def transpile_readme(readme_content, to_language):
# Function to replace bash code blocks with python code blocks
def transpile_block(match):
bash_code = match.group(1)
generator = ModifyCodeChain.from_instruction(
prompts[to_language], ChatOpenAI(model="gpt-3.5-turbo", temperature=0.2)
)
result = f"```{to_language}\n{generator.run(bash_code)}\n```"
print(result)
return result

# Use re.sub() to find and replace bash code blocks with python code blocks
return re.sub(
r"```bash\n(.*?)```", transpile_block, readme_content, flags=re.DOTALL
)
result = '```python\n' + generator.run(bash_code) + '\n```'
return result

# Read the content of README.md
with open("README.md", "r") as readme_file:
readme_content = readme_file.read()

# Define the regular expression pattern to match bash code blocks
pattern = r"```bash\n(.*?)```"

# Use re.sub() to find and replace bash code blocks with python code blocks
pythonized_content = re.sub(pattern, replace_bash_with_python, readme_content, flags=re.DOTALL)

# Write the updated content back to README.md
with open("README-python.md", "w") as readme_file:
readme_file.write(pythonized_content)

print("Bash code blocks replaced with Python code blocks in README.md")
readme_file.write(transpile_readme(readme_content, "python"))
print("Wrote README-python.md")

0 comments on commit b8339f6

Please sign in to comment.