Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,88 @@
|
|
1 |
import os
|
2 |
import sys
|
3 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
import huggingface_hub
|
6 |
-
|
7 |
import transformers
|
8 |
-
|
9 |
-
from transformers import pipeline
|
10 |
-
|
11 |
import gradio as gr
|
12 |
-
|
13 |
-
import tempfile
|
14 |
-
|
15 |
from huggingface_hub import HfFolder
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
idea = input("What is your idea for an application? ")
|
20 |
|
21 |
-
|
|
|
|
|
|
|
22 |
code = gemmacode.generate(idea)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Test the code
|
25 |
try:
|
@@ -47,10 +110,16 @@ def main():
|
|
47 |
|
48 |
# Offer the option to rebuild or deploy
|
49 |
while True:
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
if choice == "r":
|
52 |
-
# Rebuild the code
|
53 |
-
code =
|
|
|
54 |
|
55 |
# Test the code
|
56 |
try:
|
@@ -94,4 +163,4 @@ def main():
|
|
94 |
print("Invalid choice")
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
main()
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import time
|
4 |
+
import tempfile
|
5 |
+
import hashlib
|
6 |
+
import asyncio
|
7 |
+
import autopep8
|
8 |
+
from functools import lru_cache
|
9 |
+
from multiprocessing import Pool
|
10 |
+
from tenacity import retry, stop_after_attempt, wait_exponential
|
11 |
|
12 |
import huggingface_hub
|
|
|
13 |
import transformers
|
|
|
|
|
|
|
14 |
import gradio as gr
|
|
|
|
|
|
|
15 |
from huggingface_hub import HfFolder
|
16 |
|
17 |
+
# Caching Generated Code
|
18 |
+
code_cache = {}
|
|
|
19 |
|
20 |
+
def generate_code(idea):
|
21 |
+
idea_hash = hashlib.md5(idea.encode()).hexdigest()
|
22 |
+
if idea_hash in code_cache:
|
23 |
+
return code_cache[idea_hash]
|
24 |
code = gemmacode.generate(idea)
|
25 |
+
code_cache[idea_hash] = code
|
26 |
+
return code
|
27 |
+
|
28 |
+
# Parallel Processing
|
29 |
+
def generate_code_parallel(ideas):
|
30 |
+
with Pool() as pool:
|
31 |
+
return pool.map(gemmacode.generate, ideas)
|
32 |
+
|
33 |
+
# Asynchronous Code Generation
|
34 |
+
async def generate_code_async(idea):
|
35 |
+
return await gemmacode.generate_async(idea)
|
36 |
+
|
37 |
+
# Batching Requests
|
38 |
+
def batch_generate(ideas, batch_size=10):
|
39 |
+
for i in range(0, len(ideas), batch_size):
|
40 |
+
batch = ideas[i:i+batch_size]
|
41 |
+
yield gemmacode.generate_batch(batch)
|
42 |
+
|
43 |
+
# Progressive Code Generation
|
44 |
+
def generate_progressive(idea):
|
45 |
+
for partial_code in gemmacode.generate_stream(idea):
|
46 |
+
yield partial_code
|
47 |
+
# Process or display partial_code
|
48 |
+
|
49 |
+
# Optimizing Model Loading
|
50 |
+
model = None
|
51 |
+
|
52 |
+
def get_model():
|
53 |
+
global model
|
54 |
+
if model is None:
|
55 |
+
model = gemmacode.load_model()
|
56 |
+
return model
|
57 |
+
|
58 |
+
def generate_code_optimized(idea):
|
59 |
+
return get_model().generate(idea)
|
60 |
+
|
61 |
+
# Error Handling and Retries
|
62 |
+
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
63 |
+
def generate_code_with_retry(idea):
|
64 |
+
return gemmacode.generate(idea)
|
65 |
+
|
66 |
+
# Code Optimization Post-Generation
|
67 |
+
def optimize_generated_code(code):
|
68 |
+
return autopep8.fix_code(code)
|
69 |
+
|
70 |
+
# Lazy Evaluation
|
71 |
+
@lru_cache(maxsize=None)
|
72 |
+
def lazy_generate_code(idea):
|
73 |
+
return gemmacode.generate(idea)
|
74 |
+
|
75 |
+
async def main():
|
76 |
+
try:
|
77 |
+
# Get the user's idea
|
78 |
+
idea = input("What is your idea for an application? ")
|
79 |
+
except EOFError:
|
80 |
+
print("No input received. Exiting the program.")
|
81 |
+
return
|
82 |
+
|
83 |
+
# Generate the code for the application using optimized methods
|
84 |
+
code = await generate_code_async(idea)
|
85 |
+
code = optimize_generated_code(code)
|
86 |
|
87 |
# Test the code
|
88 |
try:
|
|
|
110 |
|
111 |
# Offer the option to rebuild or deploy
|
112 |
while True:
|
113 |
+
try:
|
114 |
+
choice = input("Do you want to rebuild or deploy the application? (r/d/q) ")
|
115 |
+
except EOFError:
|
116 |
+
print("No input received. Exiting the program.")
|
117 |
+
return
|
118 |
+
|
119 |
if choice == "r":
|
120 |
+
# Rebuild the code using optimized methods
|
121 |
+
code = await generate_code_async(idea)
|
122 |
+
code = optimize_generated_code(code)
|
123 |
|
124 |
# Test the code
|
125 |
try:
|
|
|
163 |
print("Invalid choice")
|
164 |
|
165 |
if __name__ == "__main__":
|
166 |
+
asyncio.run(main())
|