Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from langchain_openai import ChatOpenAI | |
| from langchain.schema import SystemMessage, HumanMessage | |
| # Define the PLAN_PROMPT | |
| PLAN_PROMPT = ( | |
| "You are an expert writer tasked with writing a high-level outline " | |
| "of a short 3-paragraph essay. Write such an outline for the user-provided topic." | |
| ) | |
| def generate_essay_outline(api_key: str, topic: str) -> str: | |
| """Generate an essay outline using the provided OpenAI API key. | |
| Args: | |
| api_key (str): The user's OpenAI API key. | |
| topic (str): The essay topic. | |
| Returns: | |
| str: Generated essay outline. | |
| """ | |
| if not api_key.strip(): | |
| return "⚠️ Please provide a valid OpenAI API key." | |
| try: | |
| # Initialize the model with the user’s API key | |
| model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, api_key=api_key) | |
| # Build conversation | |
| messages = [ | |
| SystemMessage(content=PLAN_PROMPT), | |
| HumanMessage(content=topic) | |
| ] | |
| # Get response | |
| response = model.invoke(messages) | |
| return response.content | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| # Create Gradio interface with API key + topic | |
| interface = gr.Interface( | |
| fn=generate_essay_outline, | |
| inputs=[ | |
| gr.Textbox(label="🔑 OpenAI API Key", type="password", placeholder="Enter your OpenAI API key..."), | |
| gr.Textbox(label="📘 Essay Topic", placeholder="Enter a topic for your essay...") | |
| ], | |
| outputs="text", | |
| title="Essay Outline Generator", | |
| description="Enter your OpenAI API key and a topic, and the model will generate an essay outline.", | |
| ) | |
| # Launch the interface | |
| if __name__ == "__main__": | |
| interface.launch(share=True) | |