Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# response = ""
|
| 24 |
+
# for letter in decoded_code: #[decoded_code[i:i+1] for i in range(0, len(decoded_code), 1)]:
|
| 25 |
+
# response += letter + ""
|
| 26 |
+
# yield chat_history + [(user_input, response)]
|
| 27 |
+
|
| 28 |
+
my_theme = gr.Theme.from_hub('finlaymacklon/boxy_violet')
|
| 29 |
+
with gr.Blocks(title="Python Code Generation",theme=my_theme) as demo:
|
| 30 |
+
gr.HTML(value="<style>h1 {text-align: center;}</style><h1>Python Code Generation</h1>")
|
| 31 |
+
chatbot = gr.Chatbot([], elem_id="chatbot")
|
| 32 |
+
message = gr.Textbox(label="Write a python script to..",placeholder="Eg. Check if a number is prime")
|
| 33 |
+
message.submit(chat, [chatbot, message], chatbot)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
demo.queue().launch(enable_queue=True)
|