File size: 1,738 Bytes
8d71d10
0eeb585
8d71d10
 
 
 
a661a39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d71d10
a661a39
 
 
 
 
 
 
 
8d71d10
 
 
a661a39
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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)