Skip to content

Commit c5dbb69

Browse files
committed
add examples of file usage
1 parent f902b40 commit c5dbb69

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

examples/code_interpreter_demo.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,112 @@
5151
print(f"{output.type}: {output.data}")
5252
if response.data.errors:
5353
print(f"Errors: {response.data.errors}")
54+
55+
# Example 4: Uploading and using a file
56+
print("Example 4: Uploading and using a file")
57+
58+
# Define the file content and structure as a dictionary
59+
file_to_upload = {
60+
"name": "data.txt",
61+
"encoding": "string",
62+
"content": "This is the content of the uploaded file.",
63+
}
64+
65+
# Code to read the uploaded file
66+
code_to_read_file = """
67+
try:
68+
with open('data.txt', 'r') as f:
69+
content = f.read()
70+
print(f"Content read from data.txt: {content}")
71+
except FileNotFoundError:
72+
print("Error: data.txt not found.")
73+
"""
74+
75+
response = code_interpreter.run(
76+
code=code_to_read_file,
77+
language="python",
78+
files=[file_to_upload], # Pass the file dictionary in a list
79+
)
80+
81+
# Print results
82+
print(f"Status: {response.data.status}")
83+
for output in response.data.outputs:
84+
print(f"{output.type}: {output.data}")
85+
if response.data.errors:
86+
print(f"Errors: {response.data.errors}")
87+
print("\n")
88+
89+
# Example 5: Uploading a script and running it
90+
print("Example 5: Uploading a python script and running it")
91+
92+
script_content = "import sys\nprint(f'Hello from {sys.argv[0]}!')"
93+
94+
# Define the script file as a dictionary
95+
script_file = {
96+
"name": "myscript.py",
97+
"encoding": "string",
98+
"content": script_content,
99+
}
100+
101+
code_to_run_script = "!python myscript.py"
102+
103+
response = code_interpreter.run(
104+
code=code_to_run_script,
105+
language="python",
106+
files=[script_file], # Pass the script dictionary in a list
107+
)
108+
109+
# Print results
110+
print(f"Status: {response.data.status}")
111+
for output in response.data.outputs:
112+
print(f"{output.type}: {output.data}")
113+
if response.data.errors:
114+
print(f"Errors: {response.data.errors}")
115+
print("\n")
116+
117+
# Example 6: Uploading a base64 encoded image (simulated)
118+
119+
print("Example 6: Uploading a base64 encoded binary file (e.g., image)")
120+
121+
# Example: A tiny 1x1 black PNG image, base64 encoded
122+
# In a real scenario, you would read your binary file and base64 encode its content
123+
tiny_png_base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
124+
125+
image_file = {
126+
"name": "tiny.png",
127+
"encoding": "base64", # Use base64 encoding for binary files
128+
"content": tiny_png_base64,
129+
}
130+
131+
# Code to check if the file exists and its size (Python doesn't inherently know image dimensions from bytes alone)
132+
code_to_check_file = """
133+
import os
134+
import base64
135+
136+
file_path = 'tiny.png'
137+
if os.path.exists(file_path):
138+
# Read the raw bytes back
139+
with open(file_path, 'rb') as f:
140+
raw_bytes = f.read()
141+
original_bytes = base64.b64decode('""" + tiny_png_base64 + """')
142+
print(f"File '{file_path}' exists.")
143+
print(f"Size on disk: {os.path.getsize(file_path)} bytes.")
144+
print(f"Size of original decoded base64 data: {len(original_bytes)} bytes.")
145+
146+
else:
147+
print(f"File '{file_path}' does not exist.")
148+
"""
149+
150+
response = code_interpreter.run(
151+
code=code_to_check_file,
152+
language="python",
153+
files=[image_file],
154+
)
155+
156+
# Print results
157+
print(f"Status: {response.data.status}")
158+
for output in response.data.outputs:
159+
print(f"{output.type}: {output.data}")
160+
if response.data.errors:
161+
print(f"Errors: {response.data.errors}")
162+
print("\n")

0 commit comments

Comments
 (0)