SoSa123456 commited on
Commit
6affcce
1 Parent(s): 485e84a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -25
app.py CHANGED
@@ -4,44 +4,66 @@ warnings.filterwarnings('ignore')
4
  import matplotlib as mpl
5
  mpl.rcParams["figure.dpi"] = 150
6
 
7
- from transformers import pipeline
8
- import gradio as gr
9
-
10
- # Initialize the language model
11
- #generator = pipeline('text-generation', model='mistralai/Mixtral-8x7B-Instruct-v0.1') #'tiiuae/falcon-7b-instruct')
12
  from huggingface_hub import InferenceClient
13
  import gradio as gr
14
 
 
15
  generator = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
16
 
17
  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):
18
- # Variables
19
- 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...
20
- 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...
21
- 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...
22
- POTENTIAL SOLUTIONS: But don't worry, there are solutions. {potential_solutions} are all steps we can take to mitigate these effects...
23
- INDIVIDUAL ROLE: 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...
24
- 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...
25
- 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!"""
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Generate the script
28
- script = generator(prompt_template, max_length=1000)[0]['generated_text']
29
 
30
- # Split the script into sections
31
- sections = script.split("\n")
32
 
33
- # Calculate the word count for each section
34
- word_counts = [len(section.split()) for section in sections]
 
 
35
 
36
- # Check if any section exceeds the target word count
37
- for i, count in enumerate(word_counts):
38
- if count > 200:
39
- print(f"Warning: Section {i+1} exceeds the target word count. You may need to shorten this section.")
40
 
41
- return script
42
 
43
  # Create a Gradio interface
44
- iface = gr.Interface(fn=generate_script, inputs=[gr.inputs.Textbox(label="Host Name"), gr.inputs.Textbox(label="Listener Location"), gr.inputs.Textbox(label="Causes Climate Change"), gr.inputs.Number(label="CO2 Level"), gr.inputs.Textbox(label="Effects Climate Change"), gr.inputs.Number(label="Sea Level Rise"), gr.inputs.Number(label="Warming Rate"), gr.inputs.Textbox(label="Potential Solutions"), gr.inputs.Textbox(label="Individual Role"), gr.inputs.Textbox(label="Call To Action")], outputs="text")
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  # Launch the interface
47
  iface.launch()
 
4
  import matplotlib as mpl
5
  mpl.rcParams["figure.dpi"] = 150
6
 
 
 
 
 
 
7
  from huggingface_hub import InferenceClient
8
  import gradio as gr
9
 
10
+ # Initialize the language model
11
  generator = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
12
 
13
  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):
14
+ # Default values for variables if not provided by the user
15
+ causes_climate_change = causes_climate_change or "default_causes"
16
+ co2_level = co2_level or 400
17
+ effects_climate_change = effects_climate_change or "default_effects"
18
+ sea_level_rise = sea_level_rise or 2.0
19
+ warming_rate = warming_rate or 1.5
20
+ potential_solutions = potential_solutions or "default_solutions"
21
+ individual_role = individual_role or "default_role"
22
+ call_to_action = call_to_action or "default_action"
23
+
24
+ # Variables
25
+ 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...
26
+ 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...
27
+ 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...
28
+ POTENTIAL SOLUTIONS: But don't worry, there are solutions. {potential_solutions} are all steps we can take to mitigate these effects...
29
+ 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...
30
+ 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...
31
+ 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!"""
32
+
33
+ # Generate the script
34
+ script = generator(prompt_template, max_length=1000)[0]['generated_text']
35
 
36
+ # Split the script into sections
37
+ sections = script.split("\n")
38
 
39
+ # Calculate the word count for each section
40
+ word_counts = [len(section.split()) for section in sections]
41
 
42
+ # Check if any section exceeds the target word count
43
+ for i, count in enumerate(word_counts):
44
+ if count > 200:
45
+ print(f"Warning: Section {i+1} exceeds the target word count. You may need to shorten this section.")
46
 
47
+ # Debug output to Gradio interface
48
+ debug_output = f"Debug Information:\n{'='*20}\n"
49
+ debug_output += f"Generated Script:\n{script}\n{'='*20}\n"
50
+ debug_output += f"Word Counts per Section:\n{word_counts}\n{'='*20}\n"
51
 
52
+ return script, debug_output
53
 
54
  # Create a Gradio interface
55
+ iface = gr.Interface(fn=generate_script, inputs=[
56
+ gr.Interface.Textbox(label="Host Name"),
57
+ gr.Interface.Textbox(label="Listener Location"),
58
+ gr.Interface.Textbox(label="Causes Climate Change", optional=True),
59
+ gr.Interface.Number(label="CO2 Level", optional=True),
60
+ gr.Interface.Textbox(label="Effects Climate Change", optional=True),
61
+ gr.Interface.Number(label="Sea Level Rise", optional=True),
62
+ gr.Interface.Number(label="Warming Rate", optional=True),
63
+ gr.Interface.Textbox(label="Potential Solutions", optional=True),
64
+ gr.Interface.Textbox(label="Individual Role", optional=True),
65
+ gr.Interface.Textbox(label="Call To Action", optional=True)
66
+ ], outputs=["text", "text"])
67
 
68
  # Launch the interface
69
  iface.launch()