Update app.py
Browse files
app.py
CHANGED
|
@@ -3,85 +3,66 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
-
from langchain_core.runnables import ConfigurableField
|
| 7 |
|
| 8 |
-
def
|
| 9 |
try:
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
model = ChatOpenAI(
|
| 15 |
-
model="gpt-4o-mini",
|
| 16 |
openai_api_key=api_key
|
| 17 |
-
).configurable_fields(
|
| 18 |
-
temperature=ConfigurableField(id="temperature")
|
| 19 |
)
|
| 20 |
-
|
| 21 |
-
# Define different prompts based on mode
|
| 22 |
-
if mode == "joke":
|
| 23 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 24 |
-
("system", "You are a helpful assistant skilled in telling jokes."),
|
| 25 |
-
("user", "Tell me a funny joke about {topic}")
|
| 26 |
-
])
|
| 27 |
-
else:
|
| 28 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 29 |
-
("system", "You are a helpful assistant skilled in writing poetry."),
|
| 30 |
-
("user", "Write a beautiful poem about {topic}")
|
| 31 |
-
])
|
| 32 |
|
| 33 |
-
# Create
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"temperature": float(temperature)
|
| 40 |
-
}
|
| 41 |
-
).invoke({"topic": topic})
|
| 42 |
|
| 43 |
return response.content
|
| 44 |
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {str(e)}"
|
| 47 |
-
finally:
|
| 48 |
-
if "OPENAI_API_KEY" in os.environ:
|
| 49 |
-
del os.environ["OPENAI_API_KEY"]
|
| 50 |
|
| 51 |
# Create Gradio interface
|
| 52 |
demo = gr.Interface(
|
| 53 |
-
fn=
|
| 54 |
inputs=[
|
| 55 |
gr.Textbox(
|
| 56 |
-
label="
|
| 57 |
-
placeholder="
|
| 58 |
-
|
| 59 |
),
|
| 60 |
gr.Slider(
|
| 61 |
minimum=0,
|
| 62 |
-
maximum=
|
| 63 |
-
value=0.
|
| 64 |
step=0.1,
|
| 65 |
-
label="Temperature (
|
| 66 |
-
),
|
| 67 |
-
gr.Radio(
|
| 68 |
-
choices=["joke", "poem"],
|
| 69 |
-
label="Style",
|
| 70 |
-
value="joke"
|
| 71 |
),
|
| 72 |
gr.Textbox(
|
| 73 |
label="OpenAI API Key",
|
| 74 |
-
placeholder="Enter your OpenAI API key",
|
| 75 |
type="password"
|
| 76 |
)
|
| 77 |
],
|
| 78 |
-
outputs=gr.Textbox(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
demo.launch(
|
|
|
|
| 3 |
import os
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 6 |
|
| 7 |
+
def generate_response(prompt, temperature, api_key):
|
| 8 |
try:
|
| 9 |
+
# Initialize model with given temperature
|
| 10 |
+
llm = ChatOpenAI(
|
| 11 |
+
model="gpt-3.5-turbo", # Using 3.5-turbo as it's more reliable
|
| 12 |
+
temperature=float(temperature),
|
|
|
|
|
|
|
| 13 |
openai_api_key=api_key
|
|
|
|
|
|
|
| 14 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Create simple prompt
|
| 17 |
+
template = ChatPromptTemplate.from_messages([
|
| 18 |
+
("system", "You are a creative assistant."),
|
| 19 |
+
("user", "Generate a creative description of {prompt}")
|
| 20 |
+
])
|
| 21 |
|
| 22 |
+
# Create and run chain
|
| 23 |
+
chain = template | llm
|
| 24 |
+
response = chain.invoke({"prompt": prompt})
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
return response.content
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Create Gradio interface
|
| 32 |
demo = gr.Interface(
|
| 33 |
+
fn=generate_response,
|
| 34 |
inputs=[
|
| 35 |
gr.Textbox(
|
| 36 |
+
label="Prompt",
|
| 37 |
+
placeholder="Example: 'a blue elephant'",
|
| 38 |
+
value="a blue elephant"
|
| 39 |
),
|
| 40 |
gr.Slider(
|
| 41 |
minimum=0,
|
| 42 |
+
maximum=2,
|
| 43 |
+
value=0.7,
|
| 44 |
step=0.1,
|
| 45 |
+
label="Temperature (0: Focused, 2: More Creative)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
),
|
| 47 |
gr.Textbox(
|
| 48 |
label="OpenAI API Key",
|
|
|
|
| 49 |
type="password"
|
| 50 |
)
|
| 51 |
],
|
| 52 |
+
outputs=gr.Textbox(label="Generated Response", lines=5),
|
| 53 |
+
title="🎨 Temperature Effect Demo",
|
| 54 |
+
description="""
|
| 55 |
+
Try the same prompt with different temperatures:
|
| 56 |
+
- Temperature 0: More focused, consistent responses
|
| 57 |
+
- Temperature 0.7: Balanced creativity
|
| 58 |
+
- Temperature 2: More random, creative responses
|
| 59 |
+
|
| 60 |
+
Example prompt: 'a blue elephant'
|
| 61 |
+
- T=0: "A blue elephant is a fictional creature with blue-colored skin..."
|
| 62 |
+
- T=0.7: "Imagine a majestic pachyderm with sapphire-tinted hide..."
|
| 63 |
+
- T=2: "Dancing through cotton candy clouds, this azure giant..."
|
| 64 |
+
"""
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|