プロンプトからコードを生成し実行結果とコードを出力します。
コードは正常な動作が確認されています。
APIキーの取得(無料)
Code Dash コードダッシュ
プロンプトから、即実行結果を表示。(Google gemini-pro を使用。corab でもローカルでも実行可能。)
生成されたコードにエラーがある場合自動で修正されます!
!pip install gradio
import subprocess
import google.generativeai as genai
import gradio as gr
from PIL import Image
# APIキーの設定
GOOGLE_API_KEY = "API_KEY"
genai.configure(api_key=GOOGLE_API_KEY)
# Generative AIモデルの初期化
model = genai.GenerativeModel('gemini-pro')
# Pythonコードを実行する関数
def execute_python_code(python_code):
try:
# Pythonコードをサブプロセスとして実行
result = subprocess.run(["python", "-c", python_code], capture_output=True, text=True, check=True)
return result.stdout, None # コマンドの出力を返す
except subprocess.CalledProcessError as e:
# エラーメッセージを返す
return None, str(e)
# プロンプトからPythonコードを生成し、実行して結果を表示する
def generate_and_execute_code(inputs):
# ユーザーからのプロンプトを受け取る
user_prompt = inputs
prompt = user_prompt + "Pythonコードを生成し、生成されたPythonコードのみをプリント"
# 言語モデルによるコード生成
generated_code = model.generate_content(prompt).text
# Markdown形式のコードを削除
generated_code = generated_code.replace("```python", "").replace("```", "")
# コードを実行し、結果を取得
output, error_message = execute_python_code(generated_code)
if error_message:
print(f"エラーが発生しました: {error_message}")
# エラーがあれば、言語モデルに修正を促す
prompt_for_correction = f"エラーが発生しました: {generated_code}{error_message}\n修正してください。修正されたPythonコードのみをプリント:"
corrected_code = model.generate_content(prompt_for_correction).text
# Markdown形式のコードを削除
corrected_code = corrected_code.replace("```python", "").replace("```", "")
print(f"修正されたコード:\n{corrected_code}")
# 修正されたコードを実行し、その結果を表示
corrected_output, corrected_error_message = execute_python_code(corrected_code)
if corrected_error_message:
print(f"修正されたコードでもエラーがあります: {corrected_error_message}")
else:
print("修正されたコードの実行結果:")
if corrected_output:
print(corrected_output)
else:
print("出力がありません。")
else:
print("実行結果:")
if output:
print(output)
else:
print("出力がありません。")
return generated_code, "plot.png" # 生成されたコードとグラフの画像ファイル名を返す
# Gradio UIの構築
iface = gr.Interface(fn=generate_and_execute_code, inputs="text", outputs=["text", "image"],
title="Python Code Generator & Executor", description="Enter prompt to generate and execute Python code.")
iface.launch()
ChatGPT Plus で使える Code Interpreter ですね。
text出力にも対応した改良コード
import subprocess
import google.generativeai as genai
import gradio as gr
from PIL import Image
# APIキーの設定
GOOGLE_API_KEY = "API_KEY"
genai.configure(api_key=GOOGLE_API_KEY)
# Generative AIモデルの初期化
model = genai.GenerativeModel('gemini-pro')
# Pythonコードを実行する関数
def execute_python_code(python_code):
try:
# Pythonコードをサブプロセスとして実行
result = subprocess.run(["python", "-c", python_code], capture_output=True, text=True, check=True)
return result.stdout, None # コマンドの出力を返す
except subprocess.CalledProcessError as e:
# エラーメッセージを返す
return None, str(e)
# プロンプトからPythonコードを生成し、実行して結果を表示する
def generate_and_execute_code(inputs):
# ユーザーからのプロンプトを受け取る
user_prompt = inputs
prompt = user_prompt + "Pythonコードを生成し、生成されたPythonコードのみをプリント"
# 言語モデルによるコード生成
generated_code = model.generate_content(prompt).text
# Markdown形式のコードを削除
generated_code = generated_code.replace("```python", "").replace("```", "")
# コードを実行し、結果を取得
output, error_message = execute_python_code(generated_code)
if error_message:
print(f"エラーが発生しました: {error_message}")
# エラーがあれば、言語モデルに修正を促す
prompt_for_correction = f"エラーが発生しました: {error_message}\n修正してください。修正されたPythonコードのみをプリント:"
corrected_code = model.generate_content(prompt_for_correction).text
# Markdown形式のコードを削除
corrected_code = corrected_code.replace("```python", "").replace("```", "")
print(f"修正されたコード:\n{corrected_code}")
# 修正されたコードを実行し、その結果を表示
corrected_output, corrected_error_message = execute_python_code(corrected_code)
if corrected_error_message:
print(f"修正されたコードでもエラーがあります: {corrected_error_message}")
else:
print("修正されたコードの実行結果:")
if corrected_output:
print(corrected_output)
else:
print("出力がありません。")
else:
print("実行結果:")
if output:
print(output)
else:
print("出力がありません。")
return generated_code, output, "plot.png" # 生成されたコード、実行結果、グラフの画像ファイル名を返す
# Gradio UIの構築
iface = gr.Interface(fn=generate_and_execute_code, inputs="text", outputs=["text", "text", "image"],
title="Python Code Generator & Executor", description="Enter prompt to generate and execute Python code.")
iface.launch()
text出力にも対応