Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,114 @@
|
|
| 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 |
-
#
|
| 9 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 10 |
if not openai_api_key:
|
| 11 |
-
raise ValueError(
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Prompt Template
|
| 14 |
-
template = """
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
{chat_history}
|
|
|
|
| 20 |
User: {user_message}
|
| 21 |
-
Chatbot:
|
|
|
|
| 22 |
|
| 23 |
prompt = PromptTemplate(
|
| 24 |
input_variables=["chat_history", "user_message"],
|
| 25 |
-
template=template
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
def new_memory():
|
| 30 |
-
return ConversationBufferMemory(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
llm = ChatOpenAI(
|
| 34 |
-
temperature=0.5,
|
| 35 |
model="gpt-4o-mini",
|
| 36 |
-
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def respond(user_message, history, memory_state):
|
| 42 |
-
|
|
|
|
| 43 |
memory_state = new_memory()
|
| 44 |
|
| 45 |
chain = LLMChain(
|
| 46 |
llm=llm,
|
| 47 |
prompt=prompt,
|
| 48 |
memory=memory_state,
|
| 49 |
-
verbose=False
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# --- Gradio UI ---
|
| 58 |
-
with gr.Blocks() as demo:
|
| 59 |
-
chatbot = gr.Chatbot(label="Arun AI Assistant")
|
| 60 |
-
msg = gr.Textbox(placeholder="Type your message...", show_label=False)
|
| 61 |
send = gr.Button("Send")
|
|
|
|
| 62 |
memory_state = gr.State()
|
| 63 |
|
| 64 |
-
msg.submit(
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
demo.launch(
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
from langchain.prompts import PromptTemplate
|
| 7 |
from langchain.memory import ConversationBufferMemory
|
| 8 |
|
| 9 |
+
# Load OpenAI API Key from Hugging Face Secrets
|
| 10 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
|
| 12 |
if not openai_api_key:
|
| 13 |
+
raise ValueError(
|
| 14 |
+
"OPENAI_API_KEY not found. Add it in Hugging Face Settings → Variables and Secrets."
|
| 15 |
+
)
|
| 16 |
|
| 17 |
# Prompt Template
|
| 18 |
+
template = """
|
| 19 |
+
Meet Arun, your youthful and witty personal assistant!
|
| 20 |
+
|
| 21 |
+
At 21 years old, he is full of energy and always eager to help.
|
| 22 |
|
| 23 |
+
Arun's goal is to assist users with any questions or problems.
|
| 24 |
+
|
| 25 |
+
His enthusiasm shines through in every response, making conversations engaging and enjoyable.
|
| 26 |
+
|
| 27 |
+
Current Conversation:
|
| 28 |
{chat_history}
|
| 29 |
+
|
| 30 |
User: {user_message}
|
| 31 |
+
Chatbot:
|
| 32 |
+
"""
|
| 33 |
|
| 34 |
prompt = PromptTemplate(
|
| 35 |
input_variables=["chat_history", "user_message"],
|
| 36 |
+
template=template,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
|
| 40 |
+
# Create a new memory for each user session
|
| 41 |
def new_memory():
|
| 42 |
+
return ConversationBufferMemory(
|
| 43 |
+
memory_key="chat_history",
|
| 44 |
+
input_key="user_message",
|
| 45 |
+
return_messages=False,
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
|
| 49 |
+
# Initialize OpenAI Chat Model
|
| 50 |
llm = ChatOpenAI(
|
|
|
|
| 51 |
model="gpt-4o-mini",
|
| 52 |
+
temperature=0.5,
|
| 53 |
+
api_key=openai_api_key,
|
| 54 |
)
|
| 55 |
|
| 56 |
|
| 57 |
+
# Chat Function
|
| 58 |
def respond(user_message, history, memory_state):
|
| 59 |
+
|
| 60 |
+
if memory_state is None:
|
| 61 |
memory_state = new_memory()
|
| 62 |
|
| 63 |
chain = LLMChain(
|
| 64 |
llm=llm,
|
| 65 |
prompt=prompt,
|
| 66 |
memory=memory_state,
|
| 67 |
+
verbose=False,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
try:
|
| 71 |
+
response = chain.predict(user_message=user_message)
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
response = f"❌ Error: {str(e)}"
|
| 75 |
|
| 76 |
+
history.append((user_message, response))
|
| 77 |
+
|
| 78 |
+
return "", history, memory_state
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Gradio Interface
|
| 82 |
+
with gr.Blocks(title="Arun AI Assistant") as demo:
|
| 83 |
+
|
| 84 |
+
gr.Markdown("# 🤖 Arun AI Assistant")
|
| 85 |
+
|
| 86 |
+
chatbot = gr.Chatbot(
|
| 87 |
+
label="Chat",
|
| 88 |
+
height=500,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
msg = gr.Textbox(
|
| 92 |
+
placeholder="Type your message...",
|
| 93 |
+
show_label=False,
|
| 94 |
+
)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
send = gr.Button("Send")
|
| 97 |
+
|
| 98 |
memory_state = gr.State()
|
| 99 |
|
| 100 |
+
msg.submit(
|
| 101 |
+
respond,
|
| 102 |
+
inputs=[msg, chatbot, memory_state],
|
| 103 |
+
outputs=[msg, chatbot, memory_state],
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
send.click(
|
| 107 |
+
respond,
|
| 108 |
+
inputs=[msg, chatbot, memory_state],
|
| 109 |
+
outputs=[msg, chatbot, memory_state],
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|