Update app.py
Browse files
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 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
],
|
| 52 |
-
outputs=gr.Textbox(
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|