arun47 commited on
Commit
491d3e9
·
verified ·
1 Parent(s): 77c039c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -13
app.py CHANGED
@@ -5,11 +5,12 @@ from langchain.chains import LLMChain
5
  from langchain.prompts import PromptTemplate
6
  from langchain.memory import ConversationBufferMemory
7
 
8
- # 🔑 API Key (must be set in Hugging Face Secrets, not here)
9
- # Hugging Face will automatically load it if you add OPENAI_API_KEY in "Settings > Variables and secrets"
10
  openai_api_key = os.getenv("OPENAI_API_KEY")
 
 
11
 
12
- # Define template
13
  template = """Meet Arun, your youthful and witty personal assistant!
14
  At 21 years old, he is full of energy and always eager to help.
15
  Arun's goal is to assist you with any questions or problems you might have.
@@ -19,25 +20,43 @@ His enthusiasm shines through in every response, making interactions enjoyable a
19
  User: {user_message}
20
  Chatbot:"""
21
 
22
- prompt = PromptTemplate(input_variables=["chat_history", "user_message"], template=template)
 
 
 
 
 
23
  memory = ConversationBufferMemory(memory_key="chat_history")
24
 
25
- # Chat model
26
- llm = ChatOpenAI(temperature=0.5, model="gpt-4o-mini", openai_api_key=openai_api_key)
27
- llm_chain = LLMChain(llm=llm, prompt=prompt, verbose=True, memory=memory)
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Chat function
30
  def get_text_response(user_message, history):
31
  response = llm_chain.predict(user_message=user_message)
32
  return response
33
 
34
- # Gradio UI
35
  demo = gr.ChatInterface(
36
  fn=get_text_response,
37
- examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"],
38
- title="Arun - AI Assistant",
39
- description="Your youthful, witty, personal chatbot"
 
 
40
  )
41
 
42
  if __name__ == "__main__":
43
- demo.launch()
 
5
  from langchain.prompts import PromptTemplate
6
  from langchain.memory import ConversationBufferMemory
7
 
8
+ # Load OpenAI key from Hugging Face secrets
 
9
  openai_api_key = os.getenv("OPENAI_API_KEY")
10
+ if not openai_api_key:
11
+ raise ValueError("OPENAI_API_KEY not found. Please set it in Hugging Face → Settings → Variables and secrets.")
12
 
13
+ # Prompt Template
14
  template = """Meet Arun, your youthful and witty personal assistant!
15
  At 21 years old, he is full of energy and always eager to help.
16
  Arun's goal is to assist you with any questions or problems you might have.
 
20
  User: {user_message}
21
  Chatbot:"""
22
 
23
+ prompt = PromptTemplate(
24
+ input_variables=["chat_history", "user_message"],
25
+ template=template
26
+ )
27
+
28
+ # Memory (Conversation history)
29
  memory = ConversationBufferMemory(memory_key="chat_history")
30
 
31
+ # LLM with API Key
32
+ llm = ChatOpenAI(
33
+ temperature=0.5,
34
+ model="gpt-4o-mini",
35
+ api_key=openai_api_key
36
+ )
37
+
38
+ # LLM Chain
39
+ llm_chain = LLMChain(
40
+ llm=llm,
41
+ prompt=prompt,
42
+ verbose=True,
43
+ memory=memory
44
+ )
45
 
46
+ # Chat Function
47
  def get_text_response(user_message, history):
48
  response = llm_chain.predict(user_message=user_message)
49
  return response
50
 
51
+ # Gradio Chat App
52
  demo = gr.ChatInterface(
53
  fn=get_text_response,
54
+ examples=[
55
+ "How are you doing?",
56
+ "What are your interests?",
57
+ "Which places do you like to visit?"
58
+ ]
59
  )
60
 
61
  if __name__ == "__main__":
62
+ demo.launch(share=True)