ziixh commited on
Commit
2c52598
·
verified ·
1 Parent(s): d52caef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -23
app.py CHANGED
@@ -1,28 +1,13 @@
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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"
@@ -48,7 +33,7 @@ def generate_smart_contract(language, 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
 
54
  # Enhance the code using Groq API
@@ -56,16 +41,107 @@ def generate_smart_contract(language, requirements):
56
 
57
  return enhanced_code
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  # Gradio interface for the app
60
  def generate_contract(language, requirements):
61
  return generate_smart_contract(language, requirements)
62
 
63
  interface = gr.Interface(
64
  fn=generate_contract,
65
- inputs=["text", "text"],
66
- outputs="text",
 
 
 
67
  title="Smart Contract Generator",
68
- description="Generate smart contracts using AI."
 
69
  )
70
 
71
  # Launch the Gradio app
 
1
  # app.py
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import requests
4
  import gradio as gr
5
  import torch
6
 
7
+ # Load the Hugging Face model and tokenizer (only once)
8
+ model_name = "distilgpt2" # Smaller and faster model
 
 
 
 
 
 
 
 
 
 
 
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
11
 
12
  # Groq API configuration
13
  GROQ_API_KEY = "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"
 
33
 
34
  # Use the Hugging Face model to generate code
35
  inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
36
+ outputs = model.generate(**inputs, max_length=150) # Reduced max_length
37
  generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
 
39
  # Enhance the code using Groq API
 
41
 
42
  return enhanced_code
43
 
44
+ # Custom CSS for a 3D CGI Figma-like feel
45
+ custom_css = """
46
+ body {
47
+ font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
48
+ background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
49
+ color: #fff;
50
+ perspective: 1000px;
51
+ overflow: hidden;
52
+ }
53
+
54
+ .gradio-container {
55
+ background: rgba(255, 255, 255, 0.1);
56
+ border-radius: 15px;
57
+ padding: 20px;
58
+ box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
59
+ backdrop-filter: blur(10px);
60
+ border: 1px solid rgba(255, 255, 255, 0.3);
61
+ transform-style: preserve-3d;
62
+ transform: rotateY(0deg) rotateX(0deg);
63
+ transition: transform 0.5s ease;
64
+ }
65
+
66
+ .gradio-container:hover {
67
+ transform: rotateY(10deg) rotateX(10deg);
68
+ }
69
+
70
+ .gradio-input, .gradio-output {
71
+ background: rgba(255, 255, 255, 0.2);
72
+ border: none;
73
+ border-radius: 10px;
74
+ padding: 10px;
75
+ color: #fff;
76
+ transform-style: preserve-3d;
77
+ transition: transform 0.3s ease;
78
+ }
79
+
80
+ .gradio-input:focus, .gradio-output:focus {
81
+ background: rgba(255, 255, 255, 0.3);
82
+ outline: none;
83
+ transform: translateZ(20px);
84
+ }
85
+
86
+ .gradio-button {
87
+ background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
88
+ border: none;
89
+ border-radius: 10px;
90
+ color: #fff;
91
+ padding: 10px 20px;
92
+ font-size: 16px;
93
+ cursor: pointer;
94
+ transition: background 0.3s ease, transform 0.3s ease;
95
+ transform-style: preserve-3d;
96
+ }
97
+
98
+ .gradio-button:hover {
99
+ background: linear-gradient(135deg, #2575fc 0%, #6a11cb 100%);
100
+ transform: translateZ(10px);
101
+ }
102
+
103
+ h1 {
104
+ text-align: center;
105
+ font-size: 2.5em;
106
+ margin-bottom: 20px;
107
+ background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
108
+ -webkit-background-clip: text;
109
+ -webkit-text-fill-color: transparent;
110
+ transform-style: preserve-3d;
111
+ transform: translateZ(30px);
112
+ }
113
+
114
+ @keyframes float {
115
+ 0% {
116
+ transform: translateY(0) translateZ(0);
117
+ }
118
+ 50% {
119
+ transform: translateY(-10px) translateZ(10px);
120
+ }
121
+ 100% {
122
+ transform: translateY(0) translateZ(0);
123
+ }
124
+ }
125
+
126
+ .gradio-container {
127
+ animation: float 4s ease-in-out infinite;
128
+ }
129
+ """
130
+
131
  # Gradio interface for the app
132
  def generate_contract(language, requirements):
133
  return generate_smart_contract(language, requirements)
134
 
135
  interface = gr.Interface(
136
  fn=generate_contract,
137
+ inputs=[
138
+ gr.Textbox(label="Programming Language", placeholder="e.g., Solidity"),
139
+ gr.Textbox(label="Requirements", placeholder="e.g., ERC20 token with minting functionality")
140
+ ],
141
+ outputs=gr.Textbox(label="Generated Smart Contract"),
142
  title="Smart Contract Generator",
143
+ description="Generate smart contracts using AI.",
144
+ css=custom_css
145
  )
146
 
147
  # Launch the Gradio app