Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,28 @@
|
|
| 1 |
# app.py
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
model_name = "gpt2" # Smaller and faster model
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Groq API configuration
|
| 13 |
GROQ_API_KEY = "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"
|
|
@@ -32,7 +47,7 @@ def generate_smart_contract(language, requirements):
|
|
| 32 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
| 33 |
|
| 34 |
# Use the Hugging Face model to generate code
|
| 35 |
-
inputs = tokenizer(prompt, return_tensors="pt").to("cuda"
|
| 36 |
outputs = model.generate(**inputs, max_length=300) # Reduced max_length
|
| 37 |
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
import requests
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Check if CUDA is available
|
| 8 |
+
if torch.cuda.is_available():
|
| 9 |
+
# Configure 8-bit quantization
|
| 10 |
+
quantization_config = BitsAndBytesConfig(
|
| 11 |
+
load_in_8bit=True,
|
| 12 |
+
llm_int8_threshold=6.0
|
| 13 |
+
)
|
| 14 |
+
else:
|
| 15 |
+
# Skip quantization if CUDA is not available
|
| 16 |
+
quantization_config = None
|
| 17 |
+
|
| 18 |
+
# Load the Hugging Face model and tokenizer
|
| 19 |
model_name = "gpt2" # Smaller and faster model
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
model_name,
|
| 23 |
+
quantization_config=quantization_config,
|
| 24 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 25 |
+
)
|
| 26 |
|
| 27 |
# Groq API configuration
|
| 28 |
GROQ_API_KEY = "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"
|
|
|
|
| 47 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
| 48 |
|
| 49 |
# Use the Hugging Face model to generate code
|
| 50 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 51 |
outputs = model.generate(**inputs, max_length=300) # Reduced max_length
|
| 52 |
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
|