Spaces:
Sleeping
Sleeping
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() |