Spaces:
Sleeping
Sleeping
File size: 6,224 Bytes
7ec2562 1fe6426 dfd0c40 7ec2562 9e9de0c 7ec2562 1fe6426 6c23874 1fe6426 4788f1f 7ec2562 4788f1f c2bd523 4788f1f 1e9302e 7ec2562 70109b4 7ec2562 7ac76c2 7ec2562 1fe6426 e539dbc 1fe6426 7ec2562 1fe6426 7ec2562 b815aae 7ec2562 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 9c27a00 e539dbc 7ec2562 b815aae 7ec2562 e539dbc 9c27a00 7ec2562 70109b4 e539dbc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
import gradio as gr
import logging
import torch
from gpt4all import GPT4All
# Configure logging
logging.basicConfig(level=logging.INFO)
# Model configuration
model_name_or_path = "mistral-7b-openorca.Q4_0.gguf"
# Use CPU if CUDA is out of memory or not available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Ensure device is CPU if no GPU is available
if device.type == "cpu":
logging.warning("CUDA is not available. Using CPU for model execution.")
def load_model():
try:
model = GPT4All(model_name_or_path)
logging.info("Model loaded successfully.")
return model
except Exception as e:
logging.error(f"Failed to load model: {e}")
raise e
model = load_model()
def explain_advantages(electricity_consumption, car_type, vehicle_choice, range_km, daily_mileage, travel_mileage):
logging.info("Function 'explain_advantages' called with inputs:")
logging.info(f"Electricity Consumption: {electricity_consumption}")
logging.info(f"Car Type: {car_type}")
logging.info(f"Vehicle Choice: {vehicle_choice}")
logging.info(f"Range (km): {range_km}")
logging.info(f"Daily Mileage (km): {daily_mileage}")
logging.info(f"Travel Mileage (km/year): {travel_mileage}")
try:
electricity_consumption = float(electricity_consumption)
range_km = float(range_km)
daily_mileage = float(daily_mileage)
travel_mileage = float(travel_mileage)
logging.info("Input conversion successful.")
except ValueError as ve:
logging.error(f"Input conversion error: {ve}")
return "Invalid input: Please enter numeric values for consumption, range, and mileage."
prompt = (
f"Given a {car_type} with electricity consumption of {electricity_consumption} kWh/100km, "
f"a range of {range_km} km, daily mileage of {daily_mileage} km, and annual mileage of {travel_mileage} km, "
f"compare the benefits of choosing a {vehicle_choice} vehicle over a conventional vehicle. "
f"Calculate potential fuel cost savings, CO2 emissions savings, and highlight additional benefits such as maintenance costs and driving experience. "
f"Assume the average electricity cost is 0.13 EUR per kWh and gasoline cost is 1.6 EUR per liter. "
f"Consider the average fuel economy for conventional vehicles to be 25 mpg and for hybrids to be 50 mpg. "
f"Provide a convincing argument with derived figures to illustrate the advantages. "
f"Provide advice to optimize the benefits of the chosen vehicle based on the input figures."
)
try:
generated_text = model.generate(
prompt,
max_tokens=1500,
temp=0.7,
top_p=0.95,
repeat_penalty=1.0
)
logging.info("Model generation successful.")
if not generated_text:
return "Model generation failed: No output text generated."
else:
return generated_text
except Exception as e:
logging.error(f"Error during model generation: {e}")
return f"An error occurred while generating the response: {e}"
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;700&display=swap');
body {
background: linear-gradient(to bottom right, #f0f0f5, #e8f7fc);
font-family: 'Montserrat', sans-serif;
color: #333333;
margin: 0;
padding: 0;
}
.gradio-container {
padding: 20px;
max-width: 700px;
margin: 0 auto;
}
label {
font-weight: 700;
color: #444444;
margin-bottom: 8px;
display: block;
}
textarea {
font-size: 16px;
font-family: 'Montserrat', sans-serif;
line-height: 1.5;
background-color: #ffffff;
border: 1px solid #cccccc;
padding: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
opacity: 0;
transform: translateY(20px) scale(0.95);
animation: popAndRoll 1.5s ease forwards;
}
@keyframes popAndRoll {
0% {
opacity: 0;
transform: translateY(20px) scale(0.95) rotateX(20deg);
}
50% {
opacity: 1;
transform: translateY(0) scale(1.02) rotateX(0deg);
}
100% {
opacity: 1;
transform: translateY(0) scale(1);
}
}
input, select, .gr-radio input[type="radio"] {
font-family: 'Montserrat', sans-serif;
font-size: 14px;
padding: 5px;
border-radius: 5px;
border: 1px solid #cccccc;
margin-bottom: 10px;
background-color: #ffffff;
}
.gr-button {
background-color: #6b9ac4;
color: #ffffff !important;
font-weight: 700;
border: none;
border-radius: 5px;
padding: 10px 15px;
cursor: pointer;
font-family: 'Montserrat', sans-serif;
transition: background-color 0.3s ease;
}
.gr-button:hover {
background-color: #588bb0;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Montserrat', sans-serif;
color: #333333;
margin-top: 0;
text-transform: uppercase;
letter-spacing: 1px;
}
.description, .title {
font-family: 'Montserrat', sans-serif;
color: #333333;
}
.description {
margin-bottom: 20px;
font-weight: 300;
}
.title {
margin-bottom: 10px;
font-weight: 700;
font-size: 24px;
letter-spacing: 2px;
text-align: center;
}
footer {
text-align: center;
color: #888888;
font-size: 12px;
margin-top: 20px;
font-family: 'Montserrat', sans-serif;
}
"""
interface = gr.Interface(
fn=explain_advantages,
inputs=[
gr.Number(label="Electricity Consumption (kWh/100km)", value=15.0),
gr.Textbox(label="Type of Car", value="Electric"),
gr.Radio(choices=["Hybrid", "Electric"], label="Hybrid/Electric"),
gr.Number(label="Range (km)", value=300.0),
gr.Number(label="Estimated Daily Mileage (km)", value=50.0),
gr.Number(label="Mileage from Travels (km/year)", value=1000.0),
],
outputs=gr.Textbox(label="Advantages of Going Fully Electric", lines=10),
title="Explaining the Advantages of Going Fully Electric",
description="Enter your vehicle parameters to understand the benefits of switching to a fully electric vehicle.",
css=custom_css
)
interface.launch() |