File size: 4,439 Bytes
66a02d5
ca1e304
ed1a33b
e6d9b29
7243644
66a02d5
6affcce
e6d9b29
1f03019
66a02d5
 
 
cc2185f
 
7243644
 
 
 
 
 
 
66a02d5
cc2185f
 
 
 
66a02d5
cc2185f
677a99e
5aa708b
 
 
 
cc2185f
 
1f03019
cc2185f
 
91f81f0
cc2185f
 
 
3bfdf65
ca1e304
cc2185f
ca1e304
cc2185f
3bfdf65
 
 
4486ffb
3bfdf65
 
 
66a02d5
cc2185f
ca1e304
899c09c
 
 
66a02d5
899c09c
66a02d5
 
899c09c
 
 
 
91f81f0
 
e049fa7
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
import warnings
from huggingface_hub import InferenceClient
import gradio as gr

warnings.filterwarnings('ignore')

# Initialize the language model
generator = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

def generate_script(host_name, listener_location, causes_climate_change, co2_level, effects_climate_change,
                    sea_level_rise, warming_rate, potential_solutions, individual_role, call_to_action,
                    TOPIC, DESCRIPTION):
    try:
        # Variables and template definitions...
        introduction_template = f"{host_name}, good morning! This is {listener_location}'s local radio station. Today we're talking about an issue that affects us all - {TOPIC}. It's a pressing issue that requires our immediate attention..."
        causes_template = f"The causes of {TOPIC} are {causes_climate_change}. Today, the level of CO2 in our atmosphere is {co2_level}, which is concerning..."
        effects_template = f"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..."
        solutions_template = f"But don't worry, there are solutions. {potential_solutions} are all steps we can take to mitigate these effects..."
        role_template = f"Each one of us plays a role in combating {TOPIC}. Even small actions can make a big difference. In fact, our location, {listener_location}, is particularly vulnerable to {TOPIC} due to its geographical features..."
        action_template = 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_template = f"In conclusion, {TOPIC} 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!"

        # Combine templates based on the DESCRIPTION
        prompt_template = f"""{introduction_template} {causes_template} {effects_template} {solutions_template} {role_template} {action_template} {summary_template}
        
        TOPIC: {TOPIC}. DESCRIPTION: {DESCRIPTION}"""

        # Generate the script using the language model
        script = generator.text_generation(prompt_template)[0]['generated_text']
        if isinstance(response, list):
           script = response[0].get('generated_text', '')
        else:
           script = response.get('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:
                return f"Warning: Section {i + 1} exceeds the target word count. You may need to shorten this section."

        return script

    except Exception as e:
        error_message = f"Error: {e}"

        # Save error log to a file
        with open("./error_log.txt", "a") as log_file:
            log_file.write(error_message + "\n")

        return error_message

# Gradio interface setup...
iface = gr.Interface(fn=generate_script,
                   inputs=[gr.Textbox(label="Host Name", value="John"),
                           gr.Textbox(label="Listener Location", value="City"),
                           gr.Textbox(label="Causes Climate Change", value="human activities"),
                           gr.Number(label="CO2 Level", value=400),
                           gr.Textbox(label="Effects Climate Change", value="rising temperatures"),
                           gr.Number(label="Sea Level Rise", value=0.1),
                           gr.Number(label="Warming Rate", value=0.2),
                           gr.Textbox(label="Potential Solutions", value="renewable energy"),
                           gr.Textbox(label="Individual Role", value="reduce carbon footprint"),
                           gr.Textbox(label="Call To Action", value="act now")],
                   outputs="text")

# Launch the interface
iface.launch(debug=True)