Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load model & tokenizer
|
| 6 |
+
MODEL_NAME = "ubiodee/Test_Plutus"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
if torch.cuda.is_available():
|
| 13 |
+
model.to("cuda")
|
| 14 |
+
|
| 15 |
+
# Response function
|
| 16 |
+
def generate_response(prompt):
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 18 |
+
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model.generate(
|
| 21 |
+
**inputs,
|
| 22 |
+
max_new_tokens=200,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
do_sample=True,
|
| 26 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 27 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 28 |
+
)
|
| 29 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
# Remove the prompt from the output to return only the answer
|
| 32 |
+
if response.startswith(prompt):
|
| 33 |
+
response = response[len(prompt):].strip()
|
| 34 |
+
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate_response,
|
| 40 |
+
inputs=gr.Textbox(label="Enter your prompt", lines=4, placeholder="Ask about Plutus..."),
|
| 41 |
+
outputs=gr.Textbox(label="Model Response"),
|
| 42 |
+
title="Cardano Plutus AI Assistant",
|
| 43 |
+
description="Write Plutus smart contracts on Cardano blockchain."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
demo.launch()
|