Falln87 commited on
Commit
5b79ee5
·
verified ·
1 Parent(s): 2fc2c18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
+
5
+ # Load the Starcoder2 model and tokenizer
6
+ model_name = "starcoder2"
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ def code_complete(prompt, max_length=256):
11
+ """
12
+ Generate code completion suggestions for the given prompt.
13
+
14
+ Args:
15
+ prompt (str): The incomplete code snippet.
16
+ max_length (int, optional): The maximum length of the generated code. Defaults to 256.
17
+
18
+ Returns:
19
+ list: A list of code completion suggestions.
20
+ """
21
+ # Tokenize the input prompt
22
+ inputs = tokenizer.encode_plus(prompt,
23
+ add_special_tokens=True,
24
+ max_length=max_length,
25
+ padding="max_length",
26
+ truncation=True,
27
+ return_attention_mask=True,
28
+ return_tensors="pt")
29
+
30
+ # Generate code completion suggestions
31
+ outputs = model.generate(inputs["input_ids"],
32
+ attention_mask=inputs["attention_mask"],
33
+ max_length=max_length)
34
+
35
+ # Decode the generated code
36
+ suggestions = []
37
+ for output in outputs:
38
+ decoded_code = tokenizer.decode(output, skip_special_tokens=True)
39
+ suggestions.append(decoded_code)
40
+
41
+ return suggestions
42
+
43
+ def code_fix(code):
44
+ """
45
+ Fix errors in the given code snippet.
46
+
47
+ Args:
48
+ code (str): The code snippet with errors.
49
+
50
+ Returns:
51
+ str: The corrected code snippet.
52
+ """
53
+ # Tokenize the input code
54
+ inputs = tokenizer.encode_plus(code,
55
+ add_special_tokens=True,
56
+ max_length=512,
57
+ padding="max_length",
58
+ truncation=True,
59
+ return_attention_mask=True,
60
+ return_tensors="pt")
61
+
62
+ # Generate corrected code
63
+ outputs = model.generate(inputs["input_ids"],
64
+ attention_mask=inputs["attention_mask"],
65
+ max_length=512)
66
+
67
+ # Decode the generated code
68
+ corrected_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
69
+
70
+ return corrected_code
71
+
72
+ def text_to_code(text, max_length=256):
73
+ """
74
+ Generate code from a natural language description.
75
+
76
+ Args:
77
+ text (str): The natural language description of the code.
78
+ max_length (int, optional): The maximum length of the generated code. Defaults to 256.
79
+
80
+ Returns:
81
+ str: The generated code.
82
+ """
83
+ # Tokenize the input text
84
+ inputs = tokenizer.encode_plus(text,
85
+ add_special_tokens=True,
86
+ max_length=max_length,
87
+ padding="max_length",
88
+ truncation=True,
89
+ return_attention_mask=True,
90
+ return_tensors="pt")
91
+
92
+ # Generate code from the input text
93
+ outputs = model.generate(inputs["input_ids"],
94
+ attention_mask=inputs["attention_mask"],
95
+ max_length=max_length)
96
+
97
+ # Decode the generated code
98
+ generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
99
+
100
+ return generated_code
101
+
102
+ # Create a Streamlit app
103
+ st.title("Codebot")
104
+ st.write("Welcome to the Codebot! You can use this app to generate code completions, fix errors in your code, or generate code from a natural language description.")
105
+
106
+ # Create a tab for code completion
107
+ code_completion_tab = st.tab("Code Completion")
108
+
109
+ with code_completion_tab:
110
+ st.write("Enter an incomplete code snippet:")
111
+ prompt_input = st.text_input("Prompt:", value="")
112
+ generate_button = st.button("Generate Completions")
113
+
114
+ if generate_button:
115
+ completions = code_complete(prompt_input)
116
+ st.write("Code completions:")
117
+ for i, completion in enumerate(completions):
118
+ st.write(f"{i+1}. {completion}")
119
+
120
+ # Create a tab for code fixing
121
+ code_fixing_tab = st.tab("Code Fixing")
122
+
123
+ with code_fixing_tab:
124
+ st.write("Enter a code snippet with errors:")
125
+ code_input = st.text_area("Code:", height=300)
126
+ fix_button = st.button("Fix Errors")
127
+
128
+ if fix_button:
129
+ corrected_code = code_fix(code_input)
130
+ st.write("Corrected code:")
131
+ st.code(corrected_code)
132
+
133
+ # Create a tab for text-to-code
134
+ text_to_code_tab = st.tab("Text-to-Code")
135
+
136
+ with text_to_code_tab:
137
+ st.write("Enter a natural language description of the code:")
138
+ text_input = st.text_input("Description:", value="")
139
+ generate_button = st.button("Generate Code")
140
+
141
+ if generate_button:
142
+ generated_code = text_to_code(text_input)
143
+ st.write("Generated code:")
144
+ st.code(generated_code)
145
+
146
+ # Run the Streamlit app
147
+ if __name__ == "__main__":
148
+ st.run()