Tonic commited on
Commit
6a8199d
1 Parent(s): 50188fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ title = "# 👋🏻Welcome to🌟Tonic's⚖️StableCode2"
7
+ description = """⚖️StableCode2 is a small sized coding llm that performs well in python ! You can also use [⚖️stabilityai/stable-code-3b](https://huggingface.co/stabilityai/stable-code-3b) by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/stablecode2?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
8
+ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to 🌟 [EasyAGI](https://github.com/tonic-ai/EasyAGI) 🤗Big thanks to Ythe folks at huggingface for the ZeroGPU 🤗
9
+ To contribute to this space make a PR with a new example or cool new use-case for this one 🤗
10
+ """
11
+ tokenizer = AutoTokenizer.from_pretrained(
12
+ "stabilityai/stable-code-3b", trust_remote_code=True)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ "stabilityai/stable-code-3b",
15
+ trust_remote_code=True,
16
+ torch_dtype="auto",
17
+ attn_implementation="flash_attention_2",
18
+ ).to("cuda" if torch.cuda.is_available() else "cpu")
19
+
20
+ @spaces.GPU
21
+ def generate_code(prompt):
22
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
23
+ tokens = model.generate(
24
+ **inputs,
25
+ max_new_tokens=650,
26
+ temperature=0.3,
27
+ do_sample=False,
28
+ )
29
+ generated_code = tokenizer.decode(tokens[0], skip_special_tokens=True)
30
+ return generated_code
31
+
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown(title)
34
+ gr.Markdown(description)
35
+ with gr.Row():
36
+ prompt = gr.Textbox(lines=2, placeholder="Enter your Python code prompt")
37
+ output = gr.Textbox(label = "⚖️StableCode2")
38
+ generate_button = gr.Button("Generate")
39
+ generate_button.click(fn=generate_code, inputs=prompt, outputs=output)
40
+
41
+ demo.launch()