Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 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 |
-
#
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
llm = ChatOpenAI(
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# Chat
|
| 30 |
def get_text_response(user_message, history):
|
| 31 |
response = llm_chain.predict(user_message=user_message)
|
| 32 |
return response
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
demo = gr.ChatInterface(
|
| 36 |
fn=get_text_response,
|
| 37 |
-
examples=[
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 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)
|