import gradio as gr from datetime import datetime, timedelta from openai import OpenAI from dotenv import load_dotenv import os # Load the OpenAI API key from the .env file load_dotenv() client = OpenAI( base_url="https://api.studio.nebius.com/v1/", api_key=os.getenv("NEBIUS_API_KEY")) def generate_motivational_quotes(prompt: str, start_date: str, end_date: str) -> dict: """ Generate motivational quotes for each day in the given date range using OpenAI's LLM. Args: prompt (str): The prompt to guide the quote generation. start_date (str): The start date in the format 'YYYY-MM-DD'. end_date (str): The end date in the format 'YYYY-MM-DD'. Returns: dict: A dictionary with dates as keys and motivational quotes as values. """ try: # Parse the start and end dates start = datetime.strptime(start_date, "%Y-%m-%d") end = datetime.strptime(end_date, "%Y-%m-%d") if start > end: return {"error": "Start date must be before or equal to end date."} # Generate quotes for each day in the range quotes = {} current_date = start while current_date <= end: response = client.chat.completions.create( model="microsoft/phi-4", max_tokens=8192, temperature=0.6, top_p=0.95, messages=[ { "role": "system", "content": "You are a helpful assistant that generates motivational quotes. Add the date of each post to the top." }, { "role": "user", "content": [ { "type": "text", "text": f"{prompt} Provide a short motivational quote for {current_date.strftime('%A, %B %d, %Y')}." } ] } ] ) # print(response.to_json()) quotes[current_date.strftime("%Y-%m-%d")] =response.choices[0].message.content.strip() current_date += timedelta(days=1) return quotes except Exception as e: return {"error": str(e)} # Create the Gradio interface demo = gr.Interface( fn=generate_motivational_quotes, inputs=[ gr.Textbox(label="Prompt", placeholder="Enter theme for the quotes (e.g., resilience, positivity)..."), gr.Textbox(label="Start Date (YYYY-MM-DD)"), gr.Textbox(label="End Date (YYYY-MM-DD)") ], outputs=gr.JSON(), title="Daily Motivational Quote Generator", description="Generate daily motivational quotes for a given date range using a custom prompt." ) # Launch the interface if __name__ == "__main__": demo.launch(mcp_server=True)