kalpesh225 commited on
Commit
a82c031
·
verified ·
1 Parent(s): aea2f6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -1,34 +1,46 @@
1
  import os
2
  import gradio as gr
3
- from langchain.chat_models import ChatOpenAI
4
- from langchain import LLMChain, PromptTemplate
 
5
  from langchain.memory import ConversationBufferMemory
6
 
7
- OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
 
 
 
8
 
 
9
  template = """Meet Riya, your youthful and witty personal assistant! At 21 years old, she's full of energy and always eager to help. Riya's goal is to assist you with any questions or problems you might have. Her enthusiasm shines through in every response, making interactions with her enjoyable and engaging.
10
  {chat_history}
11
  User: {user_message}
12
- Chatbot:"""
13
 
14
  prompt = PromptTemplate(
15
- input_variables=["chat_history", "user_message"], template=template
 
16
  )
17
 
 
18
  memory = ConversationBufferMemory(memory_key="chat_history")
19
-
20
  llm_chain = LLMChain(
21
- llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
 
 
 
 
22
  prompt=prompt,
23
- verbose=True,
24
  memory=memory,
25
  )
26
 
27
- def get_text_response(user_message,history):
28
- response = llm_chain.predict(user_message = user_message)
 
29
  return response
30
 
 
31
  demo = gr.ChatInterface(get_text_response)
32
 
33
  if __name__ == "__main__":
34
- demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
 
1
  import os
2
  import gradio as gr
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain.chains import LLMChain
5
+ from langchain.prompts import PromptTemplate
6
  from langchain.memory import ConversationBufferMemory
7
 
8
+ # Retrieve and validate OpenAI API key
9
+ OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
10
+ if not OPENAI_API_KEY:
11
+ raise ValueError("OPENAI_API_KEY environment variable is not set.")
12
 
13
+ # Define the prompt template
14
  template = """Meet Riya, your youthful and witty personal assistant! At 21 years old, she's full of energy and always eager to help. Riya's goal is to assist you with any questions or problems you might have. Her enthusiasm shines through in every response, making interactions with her enjoyable and engaging.
15
  {chat_history}
16
  User: {user_message}
17
+ Chatbot: """
18
 
19
  prompt = PromptTemplate(
20
+ input_variables=["chat_history", "user_message"],
21
+ template=template
22
  )
23
 
24
+ # Initialize memory and LLM chain
25
  memory = ConversationBufferMemory(memory_key="chat_history")
 
26
  llm_chain = LLMChain(
27
+ llm=ChatOpenAI(
28
+ openai_api_key=OPENAI_API_KEY,
29
+ temperature=0.5,
30
+ model_name="gpt-3.5-turbo"
31
+ ),
32
  prompt=prompt,
33
+ verbose=False, # Set to False for cleaner output
34
  memory=memory,
35
  )
36
 
37
+ # Define the response function for Gradio
38
+ def get_text_response(user_message, history):
39
+ response = llm_chain.predict(user_message=user_message)
40
  return response
41
 
42
+ # Create and launch Gradio interface
43
  demo = gr.ChatInterface(get_text_response)
44
 
45
  if __name__ == "__main__":
46
+ demo.launch() # Add server_name="0.0.0.0" or share=True for specific use cases #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.