Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,133 +1,99 @@
|
|
1 |
import os
|
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 |
-
outputs = self.mock_openai.trainer.predict(inputs.to_tensor(pad_to_multiple_of=self.mock_openai.config.max_length))
|
99 |
-
result = self.mock_openai.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
100 |
-
|
101 |
-
if max_tokens is not None and len(result) > max_tokens:
|
102 |
-
result = result[:max_tokens]
|
103 |
-
|
104 |
-
return result
|
105 |
-
|
106 |
-
@property
|
107 |
-
def chat(self):
|
108 |
-
"""
|
109 |
-
Get the Chat class instance with the pretrained model for text generation.
|
110 |
-
|
111 |
-
:return: The Chat class instance.
|
112 |
-
"""
|
113 |
-
return self.Chat(self)
|
114 |
-
|
115 |
-
# Example usage
|
116 |
if __name__ == "__main__":
|
117 |
-
|
118 |
-
model_args = parser.parse_args_into_dataclasses()[0]
|
119 |
-
client = MockOpenAI(model_name=model_args.model_name, max_tokens=model_args.max_tokens)
|
120 |
-
chat_completion = client.chat.Completions().create(
|
121 |
-
messages=[
|
122 |
-
{
|
123 |
-
"role": "system",
|
124 |
-
"content": "You are a helpful assistant.",
|
125 |
-
},
|
126 |
-
{
|
127 |
-
"role": "user",
|
128 |
-
"content": "What is deep learning?",
|
129 |
-
}
|
130 |
-
]
|
131 |
-
)
|
132 |
-
|
133 |
-
print(chat_completion)
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
|
5 |
+
import huggingface_hub
|
6 |
+
|
7 |
+
from huggingface_hub.commands import HfFolder
|
8 |
+
|
9 |
+
import transformers
|
10 |
+
|
11 |
+
from transformers import pipeline
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
import tempfile
|
16 |
+
|
17 |
+
from huggingface_hub import HfFolder
|
18 |
+
|
19 |
+
def main():
|
20 |
+
# Get the user's idea
|
21 |
+
idea = input("What is your idea for an application? ")
|
22 |
+
|
23 |
+
# Generate the code for the application
|
24 |
+
code = gemmacode.generate(idea)
|
25 |
+
|
26 |
+
# Test the code
|
27 |
+
try:
|
28 |
+
transformers.pipeline("text-generation")(code)
|
29 |
+
except Exception as e:
|
30 |
+
print("The code failed to run:", e)
|
31 |
+
return
|
32 |
+
|
33 |
+
# Ensure the functionality of the application
|
34 |
+
try:
|
35 |
+
gr.Interface(fn=transformers.pipeline("text-generation"), inputs=gr.Textbox(), outputs=gr.Textbox()).launch()
|
36 |
+
except Exception as e:
|
37 |
+
print("The application failed to run:", e)
|
38 |
+
return
|
39 |
+
|
40 |
+
# Provide an embedded webapp demo of the user's idea implementation
|
41 |
+
try:
|
42 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
43 |
+
hf_folder.save(code)
|
44 |
+
hf_folder.push_to_hub(repo_id="acecalisto3/gemmacode-demo", commit_message="Initial commit")
|
45 |
+
print(f"The demo is available at: https://huggingface.co/acecalisto3/gemmacode-demo")
|
46 |
+
except Exception as e:
|
47 |
+
print("The demo failed to launch:", e)
|
48 |
+
return
|
49 |
+
|
50 |
+
# Offer the option to rebuild or deploy
|
51 |
+
while True:
|
52 |
+
choice = input("Do you want to rebuild or deploy the application? (r/d/q) ")
|
53 |
+
if choice == "r":
|
54 |
+
# Rebuild the code
|
55 |
+
code = gemmacode.generate(idea)
|
56 |
+
|
57 |
+
# Test the code
|
58 |
+
try:
|
59 |
+
transformers.pipeline("text-generation")(code)
|
60 |
+
except Exception as e:
|
61 |
+
print("The code failed to run:", e)
|
62 |
+
return
|
63 |
+
|
64 |
+
# Ensure the functionality of the application
|
65 |
+
try:
|
66 |
+
gr.Interface(fn=transformers.pipeline("text-generation"), inputs=gr.Textbox(), outputs=gr.Textbox()).launch()
|
67 |
+
except Exception as e:
|
68 |
+
print("The application failed to run:", e)
|
69 |
+
return
|
70 |
+
|
71 |
+
# Provide an embedded webapp demo of the user's idea implementation
|
72 |
+
try:
|
73 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
74 |
+
hf_folder.save(code)
|
75 |
+
hf_folder.push_to_hub(repo_id="acecalisto3/gemmacode-demo", commit_message="Initial commit")
|
76 |
+
print(f"The demo is available at: https://huggingface.co/acecalisto3/gemmacode-demo")
|
77 |
+
except Exception as e:
|
78 |
+
print("The demo failed to launch:", e)
|
79 |
+
return
|
80 |
+
elif choice == "d":
|
81 |
+
# Deploy the application
|
82 |
+
try:
|
83 |
+
api_token = os.environ["HF_TOKEN"]
|
84 |
+
hub = huggingface_hub.HfApi(api_token=api_token)
|
85 |
+
hub.create_repo(name="my-app", organization="my-org")
|
86 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
87 |
+
hf_folder.save(code)
|
88 |
+
hf_folder.push_to_hub(repo_id="my-org/my-app", commit_message="Initial commit")
|
89 |
+
print("The application has been deployed to: https://huggingface.co/my-org/my-app")
|
90 |
+
except Exception as e:
|
91 |
+
print("The application failed to deploy:", e)
|
92 |
+
return
|
93 |
+
elif choice == "q":
|
94 |
+
break
|
95 |
+
else:
|
96 |
+
print("Invalid choice")
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
if __name__ == "__main__":
|
99 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|