Spaces:
Runtime error
Runtime error
import warnings | |
warnings.filterwarnings('ignore') | |
import matplotlib as mpl | |
mpl.rcParams["figure.dpi"] = 150 | |
from langchain import HuggingFaceHub | |
import gradio as gr | |
# Initialize the language model | |
llm = HuggingFaceHub(repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature":0.6}) | |
def get_response(prompt): | |
# Generate a response from the language model | |
response = llm(prompt) | |
# Return the response | |
return response | |
def word_count(section): | |
# Count the number of words in a section | |
return len(section.split()) | |
def generate_script(host_name, listener_location, climate_data, hazard_data): | |
# Variables | |
# Generate script | |
script = f""" | |
INTRODUCTION: '{get_response(f"{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: '{get_response(f"The causes of climate change are {climate_data['causes']}. Today, the level of CO2 in our atmosphere is {climate_data['co2_level']}, which is concerning...")}' | |
EFFECTS OF CLIMATE CHANGE: '{get_response(f"These activities result in {climate_data['effects']}, leading to drastic changes in our environment. For instance, sea levels are rising at a rate of {hazard_data['sea_level_rise']} per year, and global temperatures are increasing at a rate of {hazard_data['warming_rate']} per decade...")}' | |
POTENTIAL SOLUTIONS: '{get_response(f"But don't worry, there are solutions. {climate_data['solutions']} are all steps we can take to mitigate these effects...")}' | |
INDIVIDUAL ROLE: '{get_response(f"Each one of us plays a role in combatting 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: '{get_response(f"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: '{get_response(f"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!")}' | |
""" | |
# Split the script into sections | |
sections = script.split("\n") | |
# Calculate the word count for each section | |
word_counts = [word_count(section) 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.") | |
return script | |
# Create a Gradio interface | |
iface = gr.Interface(fn=generate_script, inputs=["text", "text", gr.JSON(), gr.JSON()], outputs="text") | |
# Launch the interface | |
iface.launch() |