Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ def process_with_config(topic, temperature, mode, api_key):
|
|
| 12 |
|
| 13 |
# Initialize configurable model with GPT-4o-mini
|
| 14 |
model = ChatOpenAI(
|
| 15 |
-
model="gpt-4o-mini",
|
| 16 |
openai_api_key=api_key,
|
| 17 |
temperature=0
|
| 18 |
).configurable_fields(
|
|
@@ -23,24 +23,27 @@ def process_with_config(topic, temperature, mode, api_key):
|
|
| 23 |
)
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Create configurable prompt
|
| 27 |
prompt = PromptTemplate.from_template(
|
| 28 |
-
"
|
| 29 |
).configurable_alternatives(
|
| 30 |
-
ConfigurableField(id="
|
| 31 |
-
default_key="
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# Create chain
|
| 37 |
chain = prompt | model
|
| 38 |
|
| 39 |
-
# Configure and run
|
|
|
|
| 40 |
response = chain.with_config(
|
| 41 |
configurable={
|
| 42 |
"llm_temperature": float(temperature),
|
| 43 |
-
"
|
| 44 |
}
|
| 45 |
).invoke({"topic": topic})
|
| 46 |
|
|
@@ -49,7 +52,6 @@ def process_with_config(topic, temperature, mode, api_key):
|
|
| 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 |
|
|
@@ -88,10 +90,5 @@ demo = gr.Interface(
|
|
| 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 |
-
)
|
|
|
|
| 12 |
|
| 13 |
# Initialize configurable model with GPT-4o-mini
|
| 14 |
model = ChatOpenAI(
|
| 15 |
+
model="gpt-4o-mini",
|
| 16 |
openai_api_key=api_key,
|
| 17 |
temperature=0
|
| 18 |
).configurable_fields(
|
|
|
|
| 23 |
)
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Create configurable prompt with unique keys
|
| 27 |
prompt = PromptTemplate.from_template(
|
| 28 |
+
"Generate a {mode} about {topic}"
|
| 29 |
).configurable_alternatives(
|
| 30 |
+
ConfigurableField(id="content_type"),
|
| 31 |
+
default_key="default",
|
| 32 |
+
alternatives={
|
| 33 |
+
"default": PromptTemplate.from_template("Tell me a joke about {topic}"),
|
| 34 |
+
"poetry": PromptTemplate.from_template("Write a poem about {topic}")
|
| 35 |
+
}
|
| 36 |
)
|
| 37 |
|
| 38 |
# Create chain
|
| 39 |
chain = prompt | model
|
| 40 |
|
| 41 |
+
# Configure and run with correct mode mapping
|
| 42 |
+
mode_mapping = {"joke": "default", "poem": "poetry"}
|
| 43 |
response = chain.with_config(
|
| 44 |
configurable={
|
| 45 |
"llm_temperature": float(temperature),
|
| 46 |
+
"content_type": mode_mapping[mode]
|
| 47 |
}
|
| 48 |
).invoke({"topic": topic})
|
| 49 |
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
return f"Error: {str(e)}"
|
| 54 |
finally:
|
|
|
|
| 55 |
if "OPENAI_API_KEY" in os.environ:
|
| 56 |
del os.environ["OPENAI_API_KEY"]
|
| 57 |
|
|
|
|
| 90 |
description="Generate content using GPT-4o-mini with configurable temperature and mode"
|
| 91 |
)
|
| 92 |
|
|
|
|
| 93 |
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|