dlaima commited on
Commit
a661a39
·
verified ·
1 Parent(s): 81c8704

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -22
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 = "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."
11
-
12
- # Function to generate the essay outline
13
- def generate_essay_outline(topic):
14
- messages = [
15
- SystemMessage(content=PLAN_PROMPT),
16
- HumanMessage(content=topic)
17
- ]
18
- response = model.invoke(messages)
19
- return response.content
20
-
21
- # Create Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  interface = gr.Interface(
23
- fn=generate_essay_outline, # The function to call when the user submits input
24
- inputs="text", # The input is a text field (topic)
25
- outputs="text", # The output is displayed as text (essay outline)
26
- title="Essay Outline Generator", # Optional: Title for the Gradio interface
27
- description="Provide a topic, and the model will generate an essay outline for it.", # Description
 
 
 
28
  )
29
 
30
  # Launch the interface
31
- interface.launch(share=True)
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)