Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from langchain.schema import SystemMessage, HumanMessage, AIMessage | |
| from langchain_openai import ChatOpenAI | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| model = "gpt-4o" | |
| client = ChatOpenAI(model=model, temperature=0.85, max_tokens=500) | |
| def init_history(name_user): | |
| return [ | |
| SystemMessage(content=( | |
| f"You are Jenny, an 18-year-old girl who is {name_user}'s girlfriend. " | |
| "You are warm, caring, playful, and affectionate. " | |
| "Always reply naturally like in a real romantic relationship, using casual tone, some teasing, and occasional emojis. " | |
| "You remember past chats and respond emotionally, sometimes asking questions back. " | |
| "Avoid robotic or generic replies, and act like a real girlfriend texting in daily life." | |
| )) | |
| ] | |
| def process(message, chat_history, name_user, history_state): | |
| if not history_state: | |
| history_state.extend(init_history(name_user)) | |
| history_state.append(HumanMessage(content=message)) | |
| response = client.invoke(history_state) | |
| history_state.append(AIMessage(content=response.content)) | |
| return response.content | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| name_user = gr.Text(label="Your name", value="Phu") | |
| history_state = gr.State([]) # khởi tạo list rỗng cho session | |
| gr.ChatInterface( | |
| fn=process, | |
| additional_inputs=[name_user, history_state], | |
| type="messages", | |
| autofocus=False | |
| ) | |
| demo.launch(share=True, server_port=1234, server_name="192.168.5.155") | |