Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
-
|
| 2 |
import yaml
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
quotes = [
|
| 11 |
"Believe you can and you're halfway there.",
|
| 12 |
"Your limitation—it's only your imagination.",
|
|
@@ -17,59 +25,60 @@ def get_motivational_quote() -> str:
|
|
| 17 |
import random
|
| 18 |
return random.choice(quotes)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
agent
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
description="An agent that provides motivational quotes to inspire users.",
|
| 46 |
-
prompt_templates=prompt_templates
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
# Define chat function
|
| 50 |
def chat_with_agent(user_input, history):
|
| 51 |
if not user_input.strip():
|
| 52 |
return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
history.append({"role": "user", "content": user_input})
|
| 60 |
history.append({"role": "assistant", "content": bot_response})
|
| 61 |
-
|
| 62 |
return history
|
| 63 |
|
|
|
|
| 64 |
# Create Gradio Chat UI
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("### Welcome to MotivationAgent! 🌟\nType a message to receive a motivational quote or an uplifting response.")
|
| 67 |
-
|
| 68 |
chatbot = gr.Chatbot(label="MotivationAgent", type="messages")
|
| 69 |
user_input = gr.Textbox(placeholder="Ask me for motivation... (e.g., 'I need inspiration')")
|
| 70 |
-
|
| 71 |
submit_btn = gr.Button("Send")
|
| 72 |
submit_btn.click(chat_with_agent, inputs=[user_input, chatbot], outputs=chatbot)
|
| 73 |
|
| 74 |
-
demo.launch()
|
| 75 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import yaml
|
| 3 |
import gradio as gr
|
| 4 |
+
from langchain.agents import initialize_agent, Tool
|
| 5 |
+
from langchain.llms import HuggingFaceHub
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
|
| 8 |
+
# Load environment variables
|
| 9 |
+
HF_API_KEY = os.environ.get("HF_API_KEY")
|
| 10 |
+
HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "google/flan-t5-xxl") # Default model if env var not set
|
| 11 |
+
|
| 12 |
+
# Load prompt templates
|
| 13 |
+
with open("prompts.yaml", 'r') as stream:
|
| 14 |
+
prompt_templates = yaml.safe_load(stream)
|
| 15 |
+
|
| 16 |
+
# Define the get_motivational_quote tool
|
| 17 |
+
def get_motivational_quote():
|
| 18 |
quotes = [
|
| 19 |
"Believe you can and you're halfway there.",
|
| 20 |
"Your limitation—it's only your imagination.",
|
|
|
|
| 25 |
import random
|
| 26 |
return random.choice(quotes)
|
| 27 |
|
| 28 |
+
tools = [
|
| 29 |
+
Tool(
|
| 30 |
+
name="MotivationalQuote",
|
| 31 |
+
func=get_motivational_quote,
|
| 32 |
+
description="Useful for getting motivational quotes."
|
| 33 |
+
)
|
| 34 |
+
]
|
| 35 |
|
| 36 |
+
# Initialize the language model
|
| 37 |
+
llm = HuggingFaceHub(
|
| 38 |
+
repo_id=HF_MODEL_ID,
|
| 39 |
+
model_kwargs={"temperature": 0.7, "max_new_tokens": 512},
|
| 40 |
+
huggingfacehub_api_token=HF_API_KEY,
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
# Create the prompt template
|
| 44 |
+
prompt_template = PromptTemplate(
|
| 45 |
+
input_variables=["user_input", "tool_names", "tools"],
|
| 46 |
+
template=prompt_templates["default"] # Use the default prompt
|
| 47 |
)
|
| 48 |
|
| 49 |
+
|
| 50 |
+
# Initialize the agent
|
| 51 |
+
agent = initialize_agent(
|
| 52 |
+
tools,
|
| 53 |
+
llm,
|
| 54 |
+
agent="zero-shot-react-description", # or another suitable agent type
|
| 55 |
+
verbose=True,
|
| 56 |
+
prompt=prompt_template
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
def chat_with_agent(user_input, history):
|
| 61 |
if not user_input.strip():
|
| 62 |
return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
bot_response = agent.run(user_input)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
bot_response = f"Sorry, an error occurred: {e}"
|
| 68 |
+
|
| 69 |
history.append({"role": "user", "content": user_input})
|
| 70 |
history.append({"role": "assistant", "content": bot_response})
|
|
|
|
| 71 |
return history
|
| 72 |
|
| 73 |
+
|
| 74 |
# Create Gradio Chat UI
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown("### Welcome to MotivationAgent! 🌟\nType a message to receive a motivational quote or an uplifting response.")
|
| 77 |
+
|
| 78 |
chatbot = gr.Chatbot(label="MotivationAgent", type="messages")
|
| 79 |
user_input = gr.Textbox(placeholder="Ask me for motivation... (e.g., 'I need inspiration')")
|
| 80 |
+
|
| 81 |
submit_btn = gr.Button("Send")
|
| 82 |
submit_btn.click(chat_with_agent, inputs=[user_input, chatbot], outputs=chatbot)
|
| 83 |
|
| 84 |
+
demo.launch()
|
|
|