Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,81 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import spaces
|
| 5 |
+
# import pythonexample
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
pythonexample = """import gradio as gr
|
| 10 |
|
| 11 |
def greet(name):
|
| 12 |
return "Hello " + name + "!!"
|
| 13 |
|
| 14 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 15 |
+
demo.launch()"""
|
| 16 |
+
|
| 17 |
+
title = """🙋🏻♂️Welcome to Tonic's Granite Code ! """
|
| 18 |
+
description = """Granite-8B-Code-Instruct is a 8B parameter model fine tuned from Granite-8B-Code-Base on a combination of permissively licensed instruction data to enhance instruction following capabilities including logical reasoning and problem-solving skills.
|
| 19 |
+
### Join us :
|
| 20 |
+
TeamTonic is always making cool demos! Join our active builder's community on Discord: [Discord](https://discord.gg/GWpVpekp) On Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On Github: [Polytonic](https://github.com/tonic-ai) & contribute to [multitonic](https://github.com/multitonic/multitonic)
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
# Define the device and model path
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
model_path = "ibm-granite/granite-8b-code-instruct"
|
| 26 |
+
|
| 27 |
+
# Load the tokenizer and model
|
| 28 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 29 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 30 |
+
model.to(device)
|
| 31 |
+
model.eval()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Function to generate code
|
| 35 |
+
|
| 36 |
+
@spaces.GGPU
|
| 37 |
+
def generate_code(prompt, max_length):
|
| 38 |
+
# Prepare the input chat format
|
| 39 |
+
chat = [
|
| 40 |
+
{ "role": "user", "content": prompt }
|
| 41 |
+
]
|
| 42 |
+
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
| 43 |
+
# Tokenize the input text
|
| 44 |
+
input_tokens = tokenizer(chat, return_tensors="pt")
|
| 45 |
+
# Transfer tokenized inputs to the device (GPU)
|
| 46 |
+
for i in input_tokens:
|
| 47 |
+
input_tokens[i] = input_tokens[i].to("cuda")
|
| 48 |
+
# Generate output tokens
|
| 49 |
+
output_tokens = model.generate(**input_tokens, max_new_tokens=max_length)
|
| 50 |
+
# Decode output tokens into text
|
| 51 |
+
output_text = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)
|
| 52 |
+
# Return the generated code
|
| 53 |
+
return output_text[0]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Define Gradio Blocks
|
| 57 |
+
|
| 58 |
+
def gradio_interface():
|
| 59 |
+
|
| 60 |
+
with gr.Blocks() as interface:
|
| 61 |
+
|
| 62 |
+
gr.Markdown(title)
|
| 63 |
+
gr.Markdown(description)
|
| 64 |
+
|
| 65 |
+
# Create input and output components
|
| 66 |
+
prompt_input = gr.Code(label="Enter your Coding Question", value=pythonexample, language='python', lines=10)
|
| 67 |
+
code_output = gr.Code(label="🪨Granite Output", language='python', lines=10, interactive=True)
|
| 68 |
+
max_length_slider = gr.Slider(minimum=1, maximum=2000, value=1000, label="Max Token Length")
|
| 69 |
+
|
| 70 |
+
# Create a button to trigger code generation
|
| 71 |
+
generate_button = gr.Button("Generate Code")
|
| 72 |
+
# Define the function to be called when the button is clicked
|
| 73 |
+
generate_button.click(generate_code, inputs=[prompt_input, max_length_slider], outputs=code_output)
|
| 74 |
+
|
| 75 |
+
return interface
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
# Create and launch the Gradio interface
|
| 80 |
+
interface = gradio_interface()
|
| 81 |
+
interface.launch()
|