Spaces:
Build error
Build error
Commit
·
00da74c
1
Parent(s):
adbe025
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import TFT5ForConditionalGeneration, RobertaTokenizer
|
| 3 |
+
|
| 4 |
+
# load saved finetuned model
|
| 5 |
+
model = TFT5ForConditionalGeneration.from_pretrained('ThoughtFocusAI/CodeGeneration-CodeT5-base')
|
| 6 |
+
# load saved tokenizer
|
| 7 |
+
tokenizer = RobertaTokenizer.from_pretrained('ThoughtFocusAI/CodeGeneration-CodeT5-base')
|
| 8 |
+
|
| 9 |
+
def chat(chat_history, user_input):
|
| 10 |
+
query = "Generate Python: " + user_input
|
| 11 |
+
encoded_text = tokenizer(query, return_tensors='tf', padding='max_length', truncation=True, max_length=48)
|
| 12 |
+
|
| 13 |
+
# inference
|
| 14 |
+
generated_code = model.generate(
|
| 15 |
+
encoded_text["input_ids"], attention_mask=encoded_text["attention_mask"],
|
| 16 |
+
max_length=128
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# decode generated tokens
|
| 20 |
+
decoded_code = tokenizer.decode(generated_code.numpy()[0], skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
return chat_history + [(user_input, "<pre><code>"+decoded_code+"</code></pre>")]
|
| 23 |
+
|
| 24 |
+
my_theme = gr.Theme.from_hub('finlaymacklon/boxy_violet')
|
| 25 |
+
with gr.Blocks(title="Python Code Generation",theme=my_theme) as demo:
|
| 26 |
+
gr.HTML(value="<style>h1 {text-align: center;}</style><h1>Python Code Generation</h1>")
|
| 27 |
+
chatbot = gr.Chatbot([], elem_id="chatbot")
|
| 28 |
+
message = gr.Textbox(label="Write a python script to..",placeholder="Eg. Check if a number is prime")
|
| 29 |
+
message.submit(chat, [chatbot, message], chatbot)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
demo.queue().launch(enable_queue=True)
|