Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
#from langchain.chat_models import ChatOpenAI
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain.schema import SystemMessage, HumanMessage
|
| 5 |
|
| 6 |
-
# Initialize the model
|
| 7 |
-
model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
|
| 8 |
|
| 9 |
# Define the PLAN_PROMPT
|
| 10 |
-
PLAN_PROMPT =
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
interface = gr.Interface(
|
| 23 |
-
fn=generate_essay_outline,
|
| 24 |
-
inputs=
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
# Launch the interface
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
from langchain.schema import SystemMessage, HumanMessage
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Define the PLAN_PROMPT
|
| 7 |
+
PLAN_PROMPT = (
|
| 8 |
+
"You are an expert writer tasked with writing a high-level outline "
|
| 9 |
+
"of a short 3-paragraph essay. Write such an outline for the user-provided topic."
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate_essay_outline(api_key: str, topic: str) -> str:
|
| 14 |
+
"""Generate an essay outline using the provided OpenAI API key.
|
| 15 |
+
Args:
|
| 16 |
+
api_key (str): The user's OpenAI API key.
|
| 17 |
+
topic (str): The essay topic.
|
| 18 |
+
Returns:
|
| 19 |
+
str: Generated essay outline.
|
| 20 |
+
"""
|
| 21 |
+
if not api_key.strip():
|
| 22 |
+
return "⚠️ Please provide a valid OpenAI API key."
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Initialize the model with the user’s API key
|
| 26 |
+
model = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, api_key=api_key)
|
| 27 |
+
|
| 28 |
+
# Build conversation
|
| 29 |
+
messages = [
|
| 30 |
+
SystemMessage(content=PLAN_PROMPT),
|
| 31 |
+
HumanMessage(content=topic)
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Get response
|
| 35 |
+
response = model.invoke(messages)
|
| 36 |
+
return response.content
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"❌ Error: {str(e)}"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# Create Gradio interface with API key + topic
|
| 43 |
interface = gr.Interface(
|
| 44 |
+
fn=generate_essay_outline,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Textbox(label="🔑 OpenAI API Key", type="password", placeholder="Enter your OpenAI API key..."),
|
| 47 |
+
gr.Textbox(label="📘 Essay Topic", placeholder="Enter a topic for your essay...")
|
| 48 |
+
],
|
| 49 |
+
outputs="text",
|
| 50 |
+
title="Essay Outline Generator",
|
| 51 |
+
description="Enter your OpenAI API key and a topic, and the model will generate an essay outline.",
|
| 52 |
)
|
| 53 |
|
| 54 |
# Launch the interface
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
interface.launch(share=True)
|