jesusvilela commited on
Commit
e539dbc
·
verified ·
1 Parent(s): 9c27a00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -19
app.py CHANGED
@@ -16,7 +16,6 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
  if device.type == "cpu":
17
  logging.warning("CUDA is not available. Using CPU for model execution.")
18
 
19
- # Load the model
20
  def load_model():
21
  try:
22
  model = GPT4All(model_name_or_path)
@@ -37,7 +36,6 @@ def explain_advantages(electricity_consumption, car_type, vehicle_choice, range_
37
  logging.info(f"Daily Mileage (km): {daily_mileage}")
38
  logging.info(f"Travel Mileage (km/year): {travel_mileage}")
39
 
40
- # Input validation
41
  try:
42
  electricity_consumption = float(electricity_consumption)
43
  range_km = float(range_km)
@@ -48,7 +46,6 @@ def explain_advantages(electricity_consumption, car_type, vehicle_choice, range_
48
  logging.error(f"Input conversion error: {ve}")
49
  return "Invalid input: Please enter numeric values for consumption, range, and mileage."
50
 
51
- # Construct the prompt
52
  prompt = (
53
  f"Given a {car_type} with electricity consumption of {electricity_consumption} kWh/100km, "
54
  f"a range of {range_km} km, daily mileage of {daily_mileage} km, and annual mileage of {travel_mileage} km, "
@@ -59,20 +56,17 @@ def explain_advantages(electricity_consumption, car_type, vehicle_choice, range_
59
  f"Provide a convincing argument with derived figures to illustrate the advantages. "
60
  f"Provide advice to optimize the benefits of the chosen vehicle based on the input figures."
61
  )
62
- logging.info("Prompt constructed successfully.")
63
 
64
- # Generate the response
65
  try:
66
  generated_text = model.generate(
67
  prompt,
68
- max_tokens=1500, # Adjusted token limit
69
  temp=0.7,
70
  top_p=0.95,
71
  repeat_penalty=1.0
72
  )
73
  logging.info("Model generation successful.")
74
 
75
- # Check if the generated text is empty
76
  if not generated_text:
77
  return "Model generation failed: No output text generated."
78
  else:
@@ -81,30 +75,120 @@ def explain_advantages(electricity_consumption, car_type, vehicle_choice, range_
81
  logging.error(f"Error during model generation: {e}")
82
  return f"An error occurred while generating the response: {e}"
83
 
84
- # Define the Gradio interface with custom CSS
85
  custom_css = """
 
 
86
  body {
87
- font-family: 'Arial', sans-serif;
88
- background-color: #f5f5f5;
89
  color: #333333;
 
 
 
 
 
 
 
 
90
  }
 
 
 
 
 
 
 
 
91
  textarea {
92
  font-size: 16px;
93
- font-family: 'Roboto', sans-serif;
94
  line-height: 1.5;
95
  background-color: #ffffff;
96
  border: 1px solid #cccccc;
97
  padding: 10px;
98
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
99
- animation: fadeIn 1s ease-out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  }
101
- @keyframes fadeIn {
102
- 0% { opacity: 0; }
103
- 100% { opacity: 1; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  }
105
  """
106
 
107
- iface = gr.Interface(
108
  fn=explain_advantages,
109
  inputs=[
110
  gr.Number(label="Electricity Consumption (kWh/100km)", value=15.0),
@@ -116,9 +200,8 @@ iface = gr.Interface(
116
  ],
117
  outputs=gr.Textbox(label="Advantages of Going Fully Electric", lines=10),
118
  title="Explaining the Advantages of Going Fully Electric",
119
- description="Enter your vehicle's parameters to understand the benefits of switching to a fully electric vehicle.",
120
  css=custom_css
121
  )
122
 
123
- # Launch the app
124
- iface.launch()
 
16
  if device.type == "cpu":
17
  logging.warning("CUDA is not available. Using CPU for model execution.")
18
 
 
19
  def load_model():
20
  try:
21
  model = GPT4All(model_name_or_path)
 
36
  logging.info(f"Daily Mileage (km): {daily_mileage}")
37
  logging.info(f"Travel Mileage (km/year): {travel_mileage}")
38
 
 
39
  try:
40
  electricity_consumption = float(electricity_consumption)
41
  range_km = float(range_km)
 
46
  logging.error(f"Input conversion error: {ve}")
47
  return "Invalid input: Please enter numeric values for consumption, range, and mileage."
48
 
 
49
  prompt = (
50
  f"Given a {car_type} with electricity consumption of {electricity_consumption} kWh/100km, "
51
  f"a range of {range_km} km, daily mileage of {daily_mileage} km, and annual mileage of {travel_mileage} km, "
 
56
  f"Provide a convincing argument with derived figures to illustrate the advantages. "
57
  f"Provide advice to optimize the benefits of the chosen vehicle based on the input figures."
58
  )
 
59
 
 
60
  try:
61
  generated_text = model.generate(
62
  prompt,
63
+ max_tokens=1500,
64
  temp=0.7,
65
  top_p=0.95,
66
  repeat_penalty=1.0
67
  )
68
  logging.info("Model generation successful.")
69
 
 
70
  if not generated_text:
71
  return "Model generation failed: No output text generated."
72
  else:
 
75
  logging.error(f"Error during model generation: {e}")
76
  return f"An error occurred while generating the response: {e}"
77
 
 
78
  custom_css = """
79
+ @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');
80
+
81
  body {
82
+ background: linear-gradient(to bottom right, #f0f0f5, #e8f7fc);
83
+ font-family: 'Montserrat', sans-serif;
84
  color: #333333;
85
+ margin: 0;
86
+ padding: 0;
87
+ }
88
+
89
+ .gradio-container {
90
+ padding: 20px;
91
+ max-width: 700px;
92
+ margin: 0 auto;
93
  }
94
+
95
+ label {
96
+ font-weight: 700;
97
+ color: #444444;
98
+ margin-bottom: 8px;
99
+ display: block;
100
+ }
101
+
102
  textarea {
103
  font-size: 16px;
104
+ font-family: 'Montserrat', sans-serif;
105
  line-height: 1.5;
106
  background-color: #ffffff;
107
  border: 1px solid #cccccc;
108
  padding: 10px;
109
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
110
+ opacity: 0;
111
+ transform: translateY(20px) scale(0.95);
112
+ animation: popAndRoll 1.5s ease forwards;
113
+ }
114
+
115
+ @keyframes popAndRoll {
116
+ 0% {
117
+ opacity: 0;
118
+ transform: translateY(20px) scale(0.95) rotateX(20deg);
119
+ }
120
+ 50% {
121
+ opacity: 1;
122
+ transform: translateY(0) scale(1.02) rotateX(0deg);
123
+ }
124
+ 100% {
125
+ opacity: 1;
126
+ transform: translateY(0) scale(1);
127
+ }
128
+ }
129
+
130
+ input, select, .gr-radio input[type="radio"] {
131
+ font-family: 'Montserrat', sans-serif;
132
+ font-size: 14px;
133
+ padding: 5px;
134
+ border-radius: 5px;
135
+ border: 1px solid #cccccc;
136
+ margin-bottom: 10px;
137
+ background-color: #ffffff;
138
+ }
139
+
140
+ .gr-button {
141
+ background-color: #6b9ac4;
142
+ color: #ffffff !important;
143
+ font-weight: 700;
144
+ border: none;
145
+ border-radius: 5px;
146
+ padding: 10px 15px;
147
+ cursor: pointer;
148
+ font-family: 'Montserrat', sans-serif;
149
+ transition: background-color 0.3s ease;
150
  }
151
+
152
+ .gr-button:hover {
153
+ background-color: #588bb0;
154
+ }
155
+
156
+ h1, h2, h3, h4, h5, h6 {
157
+ font-family: 'Montserrat', sans-serif;
158
+ color: #333333;
159
+ margin-top: 0;
160
+ text-transform: uppercase;
161
+ letter-spacing: 1px;
162
+ }
163
+
164
+ .description, .title {
165
+ font-family: 'Montserrat', sans-serif;
166
+ color: #333333;
167
+ }
168
+
169
+ .description {
170
+ margin-bottom: 20px;
171
+ font-weight: 300;
172
+ }
173
+
174
+ .title {
175
+ margin-bottom: 10px;
176
+ font-weight: 700;
177
+ font-size: 24px;
178
+ letter-spacing: 2px;
179
+ text-align: center;
180
+ }
181
+
182
+ footer {
183
+ text-align: center;
184
+ color: #888888;
185
+ font-size: 12px;
186
+ margin-top: 20px;
187
+ font-family: 'Montserrat', sans-serif;
188
  }
189
  """
190
 
191
+ interface = gr.Interface(
192
  fn=explain_advantages,
193
  inputs=[
194
  gr.Number(label="Electricity Consumption (kWh/100km)", value=15.0),
 
200
  ],
201
  outputs=gr.Textbox(label="Advantages of Going Fully Electric", lines=10),
202
  title="Explaining the Advantages of Going Fully Electric",
203
+ description="Enter your vehicle parameters to understand the benefits of switching to a fully electric vehicle.",
204
  css=custom_css
205
  )
206
 
207
+ interface.launch()