Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,104 +11,127 @@ import zipfile
|
|
11 |
# f.write(os.environ['METAGPT_CONFIG_WITH_ANTHROPIC_API'])
|
12 |
|
13 |
st.title("AI Software Coder")
|
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 |
-
#
|
|
|
|
11 |
# f.write(os.environ['METAGPT_CONFIG_WITH_ANTHROPIC_API'])
|
12 |
|
13 |
st.title("AI Software Coder")
|
14 |
+
def check_password():
|
15 |
+
"""Returns `True` if the user had the correct password."""
|
16 |
+
|
17 |
+
def password_entered():
|
18 |
+
"""Checks whether a password entered by the user is correct."""
|
19 |
+
if hmac.compare_digest(st.session_state["password"], os.environ['PAGE_PASSWORD']):
|
20 |
+
st.session_state["password_correct"] = True
|
21 |
+
del st.session_state["password"] # Don't store the password.
|
22 |
+
else:
|
23 |
+
st.session_state["password_correct"] = False
|
24 |
+
|
25 |
+
# Return True if the password is validated.
|
26 |
+
if st.session_state.get("password_correct", False):
|
27 |
+
return True
|
28 |
+
|
29 |
+
# Show input for password.
|
30 |
+
st.text_input(
|
31 |
+
"Password", type="password", on_change=password_entered, key="password"
|
32 |
+
)
|
33 |
+
if "password_correct" in st.session_state:
|
34 |
+
st.error("😕 Password incorrect")
|
35 |
+
return False
|
36 |
+
|
37 |
+
|
38 |
+
if not check_password():
|
39 |
+
st.stop() # Do not continue if check_password is not True.
|
40 |
+
|
41 |
+
def generate_zip(files):
|
42 |
+
"""Generates a zip file containing the input files."""
|
43 |
+
zip_buffer = io.BytesIO()
|
44 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zip_file:
|
45 |
+
for filename, file_content in files.items():
|
46 |
+
zip_file.writestr(filename, file_content)
|
47 |
+
zip_buffer.seek(0)
|
48 |
+
return zip_buffer
|
49 |
+
|
50 |
+
text_input = st.text_area("Enter your software development requirements here:",
|
51 |
+
value="""The goal.
|
52 |
+
|
53 |
+
The instructions of the task for each file example, things to do with input and output.
|
54 |
+
|
55 |
+
Here are some examples.
|
56 |
+
|
57 |
+
<example>
|
58 |
+
### filename.json
|
59 |
+
Original document:
|
60 |
+
```markdown
|
61 |
+
## policy title
|
62 |
+
detailed section
|
63 |
+
- blah blah
|
64 |
+
```
|
65 |
+
|
66 |
+
Configuration:
|
67 |
+
```json
|
68 |
+
{
|
69 |
+
"income_limit": 50
|
70 |
+
}
|
71 |
+
```
|
72 |
+
</example>
|
73 |
+
|
74 |
+
<example>
|
75 |
+
### filename.py
|
76 |
+
Original document:
|
77 |
+
```markdown
|
78 |
+
## policy title
|
79 |
+
detailed section
|
80 |
+
- blah blah
|
81 |
+
```
|
82 |
+
|
83 |
+
Configuration:
|
84 |
+
```py
|
85 |
+
class AgeCalculator:
|
86 |
+
def __init__(self, birth_year):
|
87 |
+
self.birth_year = birth_year
|
88 |
+
|
89 |
+
def calculate_age(self, current_year):
|
90 |
+
age = current_year - self.birth_year
|
91 |
+
return self._validate_and_format_age(age)
|
92 |
+
|
93 |
+
def _validate_and_format_age(self, age):
|
94 |
+
if age < 0:
|
95 |
+
raise ValueError("Invalid age calculated")
|
96 |
+
return f"User is {age} years old"
|
97 |
+
|
98 |
+
def get_user_age(birth_year, current_year):
|
99 |
+
calculator = AgeCalculator(birth_year)
|
100 |
+
return calculator.calculate_age(current_year)
|
101 |
+
```
|
102 |
+
</example>
|
103 |
+
|
104 |
+
Here is the additional input from a lawer:
|
105 |
+
|
106 |
+
<input>
|
107 |
+
comments from Nick/Marc/...
|
108 |
+
</input>
|
109 |
+
""")
|
110 |
+
|
111 |
+
if st.button("Generate Code"):
|
112 |
+
# def download_content(url):
|
113 |
+
# response = requests.get(url)
|
114 |
+
# response.raise_for_status()
|
115 |
+
# return response.text
|
116 |
|
117 |
+
# def replace_urls_with_content(text):
|
118 |
+
# lines = text.splitlines()
|
119 |
+
# for i, line in enumerate(lines):
|
120 |
+
# if line.startswith('http://') or line.startswith('https://'):
|
121 |
+
# try:
|
122 |
+
# content = download_content(line)
|
123 |
+
# lines[i] = line.split('/')[-1] + '\n```\n' + content + '\n```'
|
124 |
+
# except requests.exceptions.RequestException as e:
|
125 |
+
# print(f"Error downloading content from {line}: {e}")
|
126 |
+
# return '\n'.join(lines)
|
127 |
|
128 |
+
# text_input = replace_urls_with_content(text_input)
|
129 |
+
# repo: ProjectRepo = generate_repo(text_input)
|
130 |
+
# zip_buffer = generate_zip(repo.all_files)
|
131 |
+
# st.download_button(
|
132 |
+
# label="Download Code Repository",
|
133 |
+
# data=zip_buffer,
|
134 |
+
# file_name="output.zip",
|
135 |
+
# mime="application/zip"
|
136 |
+
# )
|
137 |
+
st.balloons()
|