Felguk commited on
Commit
d8ec53e
·
verified ·
1 Parent(s): 56cc585

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Define the function to modify the code based on the prompt and selected model
5
+ def modify_code(file, prompt, model_name):
6
+ # Read the uploaded file
7
+ with open(file.name, 'r') as f:
8
+ code = f.read()
9
+
10
+ # Initialize the model based on the selected model name
11
+ if model_name == "CodeGPT":
12
+ generator = pipeline("text-generation", model="microsoft/CodeGPT-small-py")
13
+ elif model_name == "Codex":
14
+ generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
15
+ else:
16
+ return "Model not supported."
17
+
18
+ # Generate the modified code based on the prompt
19
+ modified_code = generator(f"{prompt}\n{code}", max_length=500, num_return_sequences=1)[0]['generated_text']
20
+
21
+ # Truncate the output to a maximum of 793,833 lines (or characters)
22
+ max_lines = 793833
23
+ if isinstance(modified_code, str):
24
+ # If the output is a string, truncate by lines or characters
25
+ lines = modified_code.splitlines()
26
+ if len(lines) > max_lines:
27
+ modified_code = "\n".join(lines[:max_lines])
28
+ elif len(modified_code) > max_lines:
29
+ modified_code = modified_code[:max_lines]
30
+
31
+ return modified_code
32
+
33
+ # Define the Gradio interface
34
+ with gr.Blocks(theme="Nymbo/Nymbo-theme") as demo:
35
+ gr.Markdown("# Code Modifier")
36
+
37
+ with gr.Row():
38
+ file_input = gr.File(label="Upload your code file")
39
+ prompt_input = gr.Textbox(label="Enter your prompt for changes")
40
+ model_selector = gr.Dropdown(label="Select a model", choices=["CodeGPT", "Codex"])
41
+
42
+ submit_button = gr.Button("Modify Code")
43
+
44
+ output = gr.Textbox(label="Modified Code", lines=10)
45
+
46
+ submit_button.click(fn=modify_code, inputs=[file_input, prompt_input, model_selector], outputs=output)
47
+
48
+ # Launch the interface
49
+ demo.launch()