hwang1 commited on
Commit
bbdc48a
ยท
verified ยท
1 Parent(s): 1445074

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import os
2
  import gradio as gr
3
- from langchain_openai import ChatOpenAI # โœ… ์ตœ์‹  ํŒจํ‚ค์ง€๋กœ ๋ณ€๊ฒฝ
4
- from langchain.schema import RunnableSequence
5
- from langchain.memory import ConversationSummaryMemory
6
  from langchain.prompts import PromptTemplate
7
 
8
  def chatgpt(api_key, prompts, background="You are a dream interpreter."):
@@ -11,20 +11,22 @@ def chatgpt(api_key, prompts, background="You are a dream interpreter."):
11
  if not api_key:
12
  return "Error: API Key is missing. Please set OPENAI_API_KEY in your environment variables."
13
 
14
- # โœ… ์ตœ์‹  LangChain ๋ฐฉ์‹ ์ ์šฉ
15
- chatgpt_chain = RunnableSequence(
16
- PromptTemplate(
 
17
  input_variables=["history", "human_input"],
18
  template=background + "\n{history}\nHuman: {human_input}\nDream Interpreter:"
19
- ) | ChatOpenAI(temperature=0, openai_api_key=api_key)
 
20
  )
21
 
22
- return chatgpt_chain.invoke({"human_input": prompts}) # โœ… invoke() ์‚ฌ์šฉ
23
 
24
  except Exception as e:
25
  return f"Error: {str(e)}"
26
 
27
- # โœ… Gradio ์ตœ์‹  ์„ค์ • ๋ฐ˜์˜
28
  background_input = gr.Textbox(label="Background", value="You are a dream interpreter.")
29
  api_key_input = gr.Textbox(label="Enter Your API Key", type="password")
30
  user_prompt_input = gr.Textbox(label="Your Prompt")
@@ -37,4 +39,4 @@ iface = gr.Interface(
37
  )
38
 
39
  if __name__ == "__main__":
40
- iface.launch(server_name="0.0.0.0", server_port=7860) # โœ… debug=True ์ œ๊ฑฐ
 
1
  import os
2
  import gradio as gr
3
+ from langchain_openai import ChatOpenAI # โœ… ์ตœ์‹  ํŒจํ‚ค์ง€ ์ ์šฉ
4
+ from langchain.chains import LLMChain
5
+ from langchain.memory import ConversationBufferWindowMemory
6
  from langchain.prompts import PromptTemplate
7
 
8
  def chatgpt(api_key, prompts, background="You are a dream interpreter."):
 
11
  if not api_key:
12
  return "Error: API Key is missing. Please set OPENAI_API_KEY in your environment variables."
13
 
14
+ # โœ… ์ตœ์‹  LangChain ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ •
15
+ chatgpt_chain = LLMChain(
16
+ llm=ChatOpenAI(temperature=0, openai_api_key=api_key),
17
+ prompt=PromptTemplate(
18
  input_variables=["history", "human_input"],
19
  template=background + "\n{history}\nHuman: {human_input}\nDream Interpreter:"
20
+ ),
21
+ memory=ConversationBufferWindowMemory(k=2),
22
  )
23
 
24
+ return chatgpt_chain.predict(human_input=prompts) # โœ… predict() ์‚ฌ์šฉ
25
 
26
  except Exception as e:
27
  return f"Error: {str(e)}"
28
 
29
+ # โœ… Gradio UI ์„ค์ •
30
  background_input = gr.Textbox(label="Background", value="You are a dream interpreter.")
31
  api_key_input = gr.Textbox(label="Enter Your API Key", type="password")
32
  user_prompt_input = gr.Textbox(label="Your Prompt")
 
39
  )
40
 
41
  if __name__ == "__main__":
42
+ iface.launch(server_name="0.0.0.0", server_port=7860) # โœ… debug=True ์ œ๊ฑฐ