SoSa123456's picture
Update app.py
3bfdf65
raw
history blame
2.65 kB
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...
# 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(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:
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)