DexterSptizu commited on
Commit
141e5c4
·
verified ·
1 Parent(s): eda4386

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -12
app.py CHANGED
@@ -1,17 +1,25 @@
1
  #https://python.langchain.com/docs/how_to/configure/
2
  import gradio as gr
 
3
  from langchain_openai import ChatOpenAI
4
  from langchain_core.prompts import PromptTemplate
5
  from langchain_core.runnables import ConfigurableField
6
 
7
  def process_with_config(topic, temperature, mode, api_key):
8
  try:
9
- # Initialize configurable model
10
- model = ChatOpenAI(temperature=0).configurable_fields(
 
 
 
 
 
 
 
11
  temperature=ConfigurableField(
12
  id="llm_temperature",
13
  name="LLM Temperature",
14
- description="Temperature for response generation"
15
  )
16
  )
17
 
@@ -21,7 +29,8 @@ def process_with_config(topic, temperature, mode, api_key):
21
  ).configurable_alternatives(
22
  ConfigurableField(id="prompt"),
23
  default_key="joke",
24
- poem=PromptTemplate.from_template("Write a poem about {topic}")
 
25
  )
26
 
27
  # Create chain
@@ -39,20 +48,50 @@ def process_with_config(topic, temperature, mode, api_key):
39
 
40
  except Exception as e:
41
  return f"Error: {str(e)}"
 
 
 
 
42
 
43
  # Create Gradio interface
44
  demo = gr.Interface(
45
  fn=process_with_config,
46
  inputs=[
47
- gr.Textbox(label="Topic", placeholder="Enter a topic..."),
48
- gr.Slider(0, 1, value=0.5, label="Temperature"),
49
- gr.Radio(["joke", "poem"], label="Mode", value="joke"),
50
- gr.Textbox(label="OpenAI API Key", type="password")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  ],
52
- outputs=gr.Textbox(label="Generated Response"),
53
- title="LangChain Configuration Demo",
54
- description="Generate content with configurable temperature and mode"
 
 
 
55
  )
56
 
 
57
  if __name__ == "__main__":
58
- demo.launch()
 
 
 
 
 
1
  #https://python.langchain.com/docs/how_to/configure/
2
  import gradio as gr
3
+ import os
4
  from langchain_openai import ChatOpenAI
5
  from langchain_core.prompts import PromptTemplate
6
  from langchain_core.runnables import ConfigurableField
7
 
8
  def process_with_config(topic, temperature, mode, api_key):
9
  try:
10
+ # Set API key
11
+ os.environ["OPENAI_API_KEY"] = api_key
12
+
13
+ # Initialize configurable model with GPT-4o-mini
14
+ model = ChatOpenAI(
15
+ model="gpt-4o-mini", # Specifically using GPT-4o-mini
16
+ openai_api_key=api_key,
17
+ temperature=0
18
+ ).configurable_fields(
19
  temperature=ConfigurableField(
20
  id="llm_temperature",
21
  name="LLM Temperature",
22
+ description="Temperature for GPT-4o-mini response generation"
23
  )
24
  )
25
 
 
29
  ).configurable_alternatives(
30
  ConfigurableField(id="prompt"),
31
  default_key="joke",
32
+ poem=PromptTemplate.from_template("Write a poem about {topic}"),
33
+ joke=PromptTemplate.from_template("Tell me a joke about {topic}")
34
  )
35
 
36
  # Create chain
 
48
 
49
  except Exception as e:
50
  return f"Error: {str(e)}"
51
+ finally:
52
+ # Clear API key from environment for security
53
+ if "OPENAI_API_KEY" in os.environ:
54
+ del os.environ["OPENAI_API_KEY"]
55
 
56
  # Create Gradio interface
57
  demo = gr.Interface(
58
  fn=process_with_config,
59
  inputs=[
60
+ gr.Textbox(
61
+ label="Topic",
62
+ placeholder="Enter a topic...",
63
+ lines=1
64
+ ),
65
+ gr.Slider(
66
+ minimum=0,
67
+ maximum=1,
68
+ value=0.5,
69
+ step=0.1,
70
+ label="Temperature (GPT-4o-mini)"
71
+ ),
72
+ gr.Radio(
73
+ choices=["joke", "poem"],
74
+ label="Mode",
75
+ value="joke"
76
+ ),
77
+ gr.Textbox(
78
+ label="OpenAI API Key",
79
+ placeholder="Enter your OpenAI API key",
80
+ type="password"
81
+ )
82
  ],
83
+ outputs=gr.Textbox(
84
+ label="Generated Response",
85
+ lines=5
86
+ ),
87
+ title="🤖 GPT-4o-mini Configuration Demo",
88
+ description="Generate content using GPT-4o-mini with configurable temperature and mode"
89
  )
90
 
91
+ # Launch the application
92
  if __name__ == "__main__":
93
+ demo.launch(
94
+ share=False,
95
+ server_name="0.0.0.0",
96
+ server_port=7860
97
+ )