File size: 6,109 Bytes
bce4c09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import modal
import os
import random
from services.session_id_generator import SessionIdGenerator

# Load environment variables only when running locally (not in Modal's cloud)
if modal.is_local():
    from dotenv import load_dotenv

    load_dotenv()

    # Set Modal credentials from .env file
    modal_token_id = os.environ.get("MODAL_TOKEN_ID")
    modal_token_secret = os.environ.get("MODAL_TOKEN_SECRET")

    if modal_token_id and modal_token_secret:
        os.environ["MODAL_TOKEN_ID"] = modal_token_id
        os.environ["MODAL_TOKEN_SECRET"] = modal_token_secret

# Define the custom Modal image with Python execution requirements
image = modal.Image.debian_slim().pip_install(
    "mistralai",
    "nest_asyncio",
    "python-dotenv",
)

app = modal.App("fries-coder", image=image)


@app.function(
    secrets=[modal.Secret.from_name("mistral-api")],
    image=image,
    retries=3,
    timeout=300,
)
async def generate_and_run_script(prompt: str, session_id: str) -> dict:
    """
    Generate a Python script using Mistral Codestral and run it

    Args:
        prompt: Description of the script to generate

    Returns:
        dict with generated code, execution output, and status
    """
    import tempfile
    import subprocess
    from mistralai import Mistral

    try:
        # Initialize Mistral client
        client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

        # Create FIM prompt structure
        prefix = "# Write a short, funny Python script that:\n"
        code_start = "import random\nimport time\n\n"
        suffix = "\n\nif __name__ == '__main__':\n    main()"

        # Generate code using FIM
        response = client.fim.complete(
            model="codestral-latest",
            prompt=f"{prefix}{prompt}\n{code_start}",
            suffix=suffix,
            temperature=0.7,
            top_p=1,
        )

        generated_code = (
            f"{code_start}{response.choices[0].message.content}{suffix}"
        )

        # Save to temporary file
        with tempfile.NamedTemporaryFile(
            mode="w", suffix=".py", delete=False
        ) as f:
            f.write(generated_code)
            temp_path = f.name

        try:
            # Run the generated script
            result = subprocess.run(
                ["python", temp_path],
                capture_output=True,
                text=True,
                timeout=10,  # 10 second timeout
            )
            output = result.stdout
            error = None
            status = "success"
        except subprocess.TimeoutExpired:
            output = "Script execution timed out after 10 seconds"
            error = "timeout"
            status = "timeout"
        except subprocess.CalledProcessError as e:
            output = e.stdout
            error = e.stderr
            status = "runtime_error"
        finally:
            # Cleanup
            os.unlink(temp_path)

        return {
            "code": generated_code,
            "output": output,
            "error": error,
            "status": status,
        }

    except Exception as e:
        return {
            "code": None,
            "output": None,
            "error": str(e),
            "status": "generation_error",
        }


# Example usage
@app.local_entrypoint()
def main(session_id=None):
    if session_id is None:
        session_id = SessionIdGenerator.create_session_id("test")
    animal = random.choice(
        [
            "cat",
            "dog",
            "fish",
            "bird",
            "giraffe",
            "turtle",
            "monkey",
            "rabbit",
            "puppy",
            "animal",
        ]
    )
    prompt = f"""
create a simple ASCII art of a {animal}.
Create ASCII art using these characters: _ - = ~ ^ \\\\ / ( ) [ ] {{ }} < > | . o O @ *
Draw the art line by line with print statements.
Write a short, funny Python script.
Use only basic Python features.
Add a joke or pun about fries in the script.
Make it light-hearted and fun.
End with a message about fries.
Make sure the script runs without errors.
"""

    try:
        print(f"\n๐Ÿค– Generating an ascii {animal} for you!")
        result = generate_and_run_script.remote(prompt, session_id)

        # print("\n๐Ÿ“ Generated Code:")
        # print("=" * 40)
        # print(result["code"])
        print("=" * 30)
        print("\n๐ŸŽฎ Code Output:")
        print("=" * 30)
        print("\n\n")
        print(result["output"])

        # print("\n" + "=" * 80)

        print("๐ŸŸ    ๐ŸŸ     ๐ŸŸ")
        print("Golden crispy Python fries")
        print("Coming right up!")
        print()
        print("Haha. Just kidding.")

        if result["code"]:
            # Save the generated code locally
            script_file = f"storyboard/{session_id}/output/fries_for_you.py"
            os.makedirs(os.path.dirname(script_file), exist_ok=True)
            with open(script_file, "w") as f:
                f.write(result["code"])
            print("\nGo here to check out your actual custom code:")
            print(f"๐Ÿ‘‰ Code saved to: {script_file}")
            print("\n\n\n")

        if result["error"]:
            print("\nโŒ Error:")
            print("=" * 40)
            print(result["error"])

        if result["error"]:
            print("Looks like there was an error during execution.")
            print("Here are some extra fries to cheer you up!")
            print("๐ŸŸ    ๐ŸŸ     ๐ŸŸ")
            print("   ๐ŸŸ     ๐ŸŸ    ")
            print("       ๐ŸŸ      ")
            print("Now with extra machine-learned crispiness.")

    except modal.exception.FunctionTimeoutError:
        print("โฐ Script execution timed out after 300 seconds and 3 tries!")
        print("Sorry but codestral is having a hard time drawing today.")
        print("Here's a timeout fry for you! ๐ŸŸ")
        print("Here are some extra fries to cheer you up!")
        print("๐ŸŸ    ๐ŸŸ     ๐ŸŸ")
        print("   ๐ŸŸ     ๐ŸŸ    ")
        print("       ๐ŸŸ      ")
        print("Now with extra machine-learned crispiness.")