import warnings warnings.filterwarnings('ignore') import matplotlib as mpl mpl.rcParams["figure.dpi"] = 150 from huggingface_hub import InferenceClient import gradio as gr # Initialize the language model generator = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") def generate_script(host_name, listener_location, causes_climate_change="", co2_level=400, effects_climate_change="", sea_level_rise=2.0, warming_rate=1.5, potential_solutions="", individual_role="", call_to_action=""): # Variables prompt_template = f"""INTRODUCTION: {host_name}, good morning! This is {listener_location}'s local radio station. Today we're talking about an issue that affects us all - climate change. It's a pressing issue that requires our immediate attention... CAUSES OF CLIMATE CHANGE: The causes of climate change are {causes_climate_change}. Today, the level of CO2 in our atmosphere is {co2_level}, which is concerning... EFFECTS OF CLIMATE CHANGE: These activities result in {effects_climate_change}, leading to drastic changes in our environment. For instance, sea levels are rising at a rate of {sea_level_rise} per year, and global temperatures are increasing at a rate of {warming_rate} per decade... POTENTIAL SOLUTIONS: But don't worry, there are solutions. {potential_solutions} are all steps we can take to mitigate these effects... INDIVIDUAL ROLE: Each one of us plays a role in combating climate change. Even small actions can make a big difference. In fact, our location, {listener_location}, is particularly vulnerable to climate change due to its geographical features... CALL TO ACTION: So, {listener_location}, why wait? Start taking steps today towards a greener future. Support local businesses that prioritize sustainability, reduce your carbon footprint, and voice your opinion to policy makers... SUMMARY: In conclusion, climate change is a serious issue that requires our immediate attention. But by understanding its causes, effects, and potential solutions, we can all play a part in mitigating its impact. Thank you for joining us today, and remember, every small action counts!""" # Generate the script script = generator(prompt_template, max_length=1000)[0]['generated_text'] # Split the script into sections sections = script.split("\n") # Calculate the word count for each section word_counts = [len(section.split()) for section in sections] # Check if any section exceeds the target word count for i, count in enumerate(word_counts): if count > 200: print(f"Warning: Section {i+1} exceeds the target word count. You may need to shorten this section.") # Debug output to Gradio interface debug_output = f"Debug Information:\n{'='*20}\n" debug_output += f"Generated Script:\n{script}\n{'='*20}\n" debug_output += f"Word Counts per Section:\n{word_counts}\n{'='*20}\n" return script, debug_output # Create Gradio inputs with default values inputs = [ gr.Textbox(label="Host Name", default=""), gr.Textbox(label="Listener Location", default=""), gr.Textbox(label="Causes Climate Change", default=""), gr.Number(label="CO2 Level", default=400), gr.Textbox(label="Effects Climate Change", default=""), gr.Number(label="Sea Level Rise", default=2.0), gr.Number(label="Warming Rate", default=1.5), gr.Textbox(label="Potential Solutions", default=""), gr.Textbox(label="Individual Role", default=""), gr.Textbox(label="Call To Action", default="") ] # Create Gradio interface iface = gr.Interface(fn=generate_script, inputs=inputs, outputs=["text", "text"]) # Launch the interface iface.launch()