File size: 1,468 Bytes
3b232a5
 
 
aea3592
3b232a5
77b6975
3b232a5
77b6975
45cd21f
77b6975
73e886d
3b232a5
 
77b6975
 
45cd21f
77b6975
3b232a5
77b6975
45cd21f
3b232a5
 
 
 
 
77b6975
3b232a5
141e5c4
45cd21f
 
141e5c4
 
 
45cd21f
77b6975
141e5c4
45cd21f
141e5c4
 
 
 
 
3b232a5
77b6975
45cd21f
 
3b232a5
 
 
77b6975
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#https://python.langchain.com/docs/how_to/configure/
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

def generate_response(prompt, temperature, api_key):
    try:
        llm = ChatOpenAI(
            model="gpt-4o-mini",
            temperature=float(temperature),
            openai_api_key=api_key
        )
        
        template = ChatPromptTemplate.from_messages([
            ("system", "You are a creative assistant."),
            ("user", "{prompt}")
        ])
        
        chain = template | llm
        return chain.invoke({"prompt": prompt}).content
        
    except Exception as e:
        return f"Error: {str(e)}"

demo = gr.Interface(
    fn=generate_response,
    inputs=[
        gr.Textbox(
            label="Enter your prompt",
            placeholder="Write something creative..."
        ),
        gr.Slider(
            minimum=0,
            maximum=1,
            value=0.7,
            step=0.1,
            label="Temperature (0: Focused, 1: Creative)"
        ),
        gr.Textbox(
            label="OpenAI API Key",
            type="password"
        )
    ],
    outputs=gr.Textbox(label="Generated Response", lines=5),
    title="Creative Text Generator",
    description="Adjust temperature to control response creativity: 0 for focused responses, 0.7 for balanced creativity, 1 for maximum creativity."
)

if __name__ == "__main__":
    demo.launch()