Upload 2 files
Browse files- app.py +54 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import transformers
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
+
import os
|
7 |
+
|
8 |
+
title = """S&T ORA"""
|
9 |
+
|
10 |
+
model_path = "bigcode/starcoder2-15b"
|
11 |
+
|
12 |
+
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
14 |
+
|
15 |
+
model = AutoModelForCausalLM.from_pretrained( model_path)
|
16 |
+
|
17 |
+
@spaces.GPU
|
18 |
+
def generate_text(prompt, temperature=0.9, max_length=1200):
|
19 |
+
# Encode the inputs
|
20 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
21 |
+
attention_mask = torch.ones(inputs.shape, dtype=torch.long)
|
22 |
+
inputs = inputs.to("cuda")
|
23 |
+
attention_mask = attention_mask.to("cuda")
|
24 |
+
outputs = model.generate(
|
25 |
+
inputs,
|
26 |
+
attention_mask=attention_mask,
|
27 |
+
max_length=max_length,
|
28 |
+
top_p=0.9,
|
29 |
+
temperature=temperature,
|
30 |
+
do_sample=True,
|
31 |
+
pad_token_id=tokenizer.eos_token_id
|
32 |
+
)
|
33 |
+
return tokenizer.decode(outputs[0])
|
34 |
+
|
35 |
+
def gradio_app():
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
gr.Markdown(title)
|
38 |
+
prompt = gr.Code(label="Enter your code prompt", value="def print_hello_world():")
|
39 |
+
with gr.Row():
|
40 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, value=0.5, label="Temperature")
|
41 |
+
max_length = gr.Slider(minimum=100, maximum=1024, step=10, value=100, label="Generate Length")
|
42 |
+
generate_btn = gr.Button("Try✨StarCoder")
|
43 |
+
output = gr.Code(label="✨StarCoder:", lines=40)
|
44 |
+
|
45 |
+
generate_btn.click(
|
46 |
+
fn=generate_text,
|
47 |
+
inputs=[prompt, temperature, max_length],
|
48 |
+
outputs=output
|
49 |
+
)
|
50 |
+
|
51 |
+
demo.launch()
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
gradio_app()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
git+https://github.com/huggingface/transformers.git
|