Spaces:
Runtime error
Runtime error
import streamlit as st | |
import random | |
from transformers import pipeline | |
import pandas as pd | |
from datetime import datetime | |
import pytz | |
generator = pipeline('text-generation', model='gpt2') | |
max_length = 50 | |
prompts = { | |
"Difficulty sleeping": [ | |
"Try keeping a consistent sleep schedule and avoid caffeine before bedtime.", | |
"Make your bedroom a comfortable and calming environment.", | |
"Avoid using electronic devices before bedtime.", | |
"Try relaxation techniques like deep breathing or meditation.", | |
"Consider talking to a healthcare provider if sleep problems persist." | |
], | |
"Time management": [ | |
"Use a planner or time-tracking app to prioritize tasks and stay on schedule.", | |
"Break down large tasks into smaller ones.", | |
"Limit multitasking and focus on one task at a time.", | |
"Delegate tasks to others when possible.", | |
"Take regular breaks and avoid overworking yourself." | |
], | |
"Stress management": [ | |
"Practice mindfulness techniques such as deep breathing or meditation.", | |
"Get regular exercise to reduce stress and improve mood.", | |
"Get enough sleep and practice good sleep habits.", | |
"Take breaks throughout the day to reduce stress levels.", | |
"Try to identify the sources of stress in your life and develop strategies to manage them." | |
] | |
} | |
def generate_prompt(prompt): | |
solution = random.choice(prompts[prompt]) | |
prompt_text = f"What can I do to {prompt.lower()}? " | |
output = generator(prompt_text, max_length=max_length, num_return_sequences=1, no_repeat_ngram_size=2, early_stopping=True) | |
output_text = output[0]['generated_text'][len(prompt_text):].strip() | |
return prompt_text, output_text, solution | |
st.title('ICL-LM Interface') | |
option = st.selectbox('Select a problem:', list(prompts.keys())) | |
if st.button('Generate Prompt and Solution'): | |
results = [] | |
for _ in range(3): | |
prompt_text, prompt, solution = generate_prompt(option) | |
results.append([prompt_text, prompt, solution]) | |
user_timezone = st.text_input("Enter your timezone (e.g., 'America/New_York'):") | |
try: | |
tz = pytz.timezone(user_timezone) | |
current_time = datetime.now(tz).strftime('%Y-%m-%d %H:%M:%S %Z') | |
except Exception: | |
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
st.warning('Invalid timezone entered. Using the server timezone.') | |
with open('results.txt', 'a') as f: | |
for result in results: | |
f.write(f"{current_time}\t{result[0]}\t{result[1]}\t{result[2]}\n") | |
df = pd.read_csv('results.txt', sep='\t', header=None, names=['Timestamp', 'Input', 'Prompt', 'Solution']) | |
st.write(df) | |