Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,40 @@
|
|
| 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 ChatPromptTemplate
|
| 6 |
|
| 7 |
def generate_response(prompt, temperature, api_key):
|
| 8 |
try:
|
| 9 |
-
# Initialize model with given temperature
|
| 10 |
llm = ChatOpenAI(
|
| 11 |
-
model="gpt-4o-mini",
|
| 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", "
|
| 20 |
])
|
| 21 |
|
| 22 |
-
# Create and run chain
|
| 23 |
chain = template | llm
|
| 24 |
-
|
| 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="
|
| 37 |
-
placeholder="
|
| 38 |
-
value="a blue elephant"
|
| 39 |
),
|
| 40 |
gr.Slider(
|
| 41 |
minimum=0,
|
| 42 |
-
maximum=
|
| 43 |
value=0.7,
|
| 44 |
step=0.1,
|
| 45 |
-
label="Temperature (0: Focused,
|
| 46 |
),
|
| 47 |
gr.Textbox(
|
| 48 |
label="OpenAI API Key",
|
|
@@ -50,18 +42,8 @@ demo = gr.Interface(
|
|
| 50 |
)
|
| 51 |
],
|
| 52 |
outputs=gr.Textbox(label="Generated Response", lines=5),
|
| 53 |
-
title="
|
| 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__":
|
|
|
|
| 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 ChatPromptTemplate
|
| 5 |
|
| 6 |
def generate_response(prompt, temperature, api_key):
|
| 7 |
try:
|
|
|
|
| 8 |
llm = ChatOpenAI(
|
| 9 |
+
model="gpt-4o-mini",
|
| 10 |
temperature=float(temperature),
|
| 11 |
openai_api_key=api_key
|
| 12 |
)
|
| 13 |
|
|
|
|
| 14 |
template = ChatPromptTemplate.from_messages([
|
| 15 |
("system", "You are a creative assistant."),
|
| 16 |
+
("user", "{prompt}")
|
| 17 |
])
|
| 18 |
|
|
|
|
| 19 |
chain = template | llm
|
| 20 |
+
return chain.invoke({"prompt": prompt}).content
|
|
|
|
|
|
|
| 21 |
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error: {str(e)}"
|
| 24 |
|
|
|
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=generate_response,
|
| 27 |
inputs=[
|
| 28 |
gr.Textbox(
|
| 29 |
+
label="Enter your prompt",
|
| 30 |
+
placeholder="Write something creative..."
|
|
|
|
| 31 |
),
|
| 32 |
gr.Slider(
|
| 33 |
minimum=0,
|
| 34 |
+
maximum=1,
|
| 35 |
value=0.7,
|
| 36 |
step=0.1,
|
| 37 |
+
label="Temperature (0: Focused, 1: Creative)"
|
| 38 |
),
|
| 39 |
gr.Textbox(
|
| 40 |
label="OpenAI API Key",
|
|
|
|
| 42 |
)
|
| 43 |
],
|
| 44 |
outputs=gr.Textbox(label="Generated Response", lines=5),
|
| 45 |
+
title="Creative Text Generator",
|
| 46 |
+
description="Adjust temperature to control response creativity: 0 for focused responses, 0.7 for balanced creativity, 1 for maximum creativity."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|