Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,44 @@
|
|
| 1 |
-
import
|
| 2 |
import yaml
|
| 3 |
-
|
| 4 |
-
from langchain.agents import initialize_agent, Tool
|
| 5 |
-
from langchain.prompts import PromptTemplate
|
| 6 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Load prompt templates (simplified)
|
| 13 |
-
prompts = {
|
| 14 |
-
"default": """Answer the user's question. If the user says they need inspiration, use the `MotivationalQuote` tool to provide them with a quote.
|
| 15 |
-
|
| 16 |
-
User: {{user_input}}
|
| 17 |
-
Agent:""" # Improved prompt
|
| 18 |
-
}
|
| 19 |
-
prompt_template = PromptTemplate(template=prompts["default"], input_variables=["user_input"])
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# Define tools
|
| 23 |
-
def get_motivational_quote():
|
| 24 |
quotes = [
|
| 25 |
"Believe you can and you're halfway there.",
|
| 26 |
"Your limitation—it's only your imagination.",
|
| 27 |
"Push yourself, because no one else is going to do it for you.",
|
| 28 |
"Great things never come from comfort zones.",
|
| 29 |
-
"Dream it. Wish it. Do it."
|
| 30 |
]
|
| 31 |
import random
|
| 32 |
return random.choice(quotes)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
]
|
| 41 |
-
|
| 42 |
-
# Initialize LLM
|
| 43 |
-
llm = HuggingFaceEndpoint(
|
| 44 |
-
repo_id=HF_MODEL_ID,
|
| 45 |
-
temperature=0.7,
|
| 46 |
-
max_new_tokens=512,
|
| 47 |
-
huggingfacehub_api_token=HF_API_KEY,
|
| 48 |
-
task="text-generation",
|
| 49 |
)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
history.append({"role": "user", "content": user_input})
|
| 70 |
-
history.append({"role": "assistant", "content": bot_response})
|
| 71 |
-
return history
|
| 72 |
|
| 73 |
-
# Gradio UI
|
| 74 |
-
with gr.Blocks() as demo:
|
| 75 |
-
gr.Markdown("### Motivational Quote Agent")
|
| 76 |
-
chatbot = gr.Chatbot(label="MotivationBot", type="messages")
|
| 77 |
-
user_input = gr.Textbox(placeholder="Enter 'I need inspiration' to get a quote.")
|
| 78 |
-
submit_btn = gr.Button("Send")
|
| 79 |
-
submit_btn.click(chat_with_agent, inputs=[user_input, chatbot], outputs=chatbot)
|
| 80 |
|
| 81 |
-
demo.launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import yaml
|
| 3 |
+
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Define a tool to generate motivational quotes
|
| 6 |
+
@tool
|
| 7 |
+
def get_motivational_quote() -> str:
|
| 8 |
+
"""Returns a motivational quote to inspire the user."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
quotes = [
|
| 10 |
"Believe you can and you're halfway there.",
|
| 11 |
"Your limitation—it's only your imagination.",
|
| 12 |
"Push yourself, because no one else is going to do it for you.",
|
| 13 |
"Great things never come from comfort zones.",
|
| 14 |
+
"Dream it. Wish it. Do it."
|
| 15 |
]
|
| 16 |
import random
|
| 17 |
return random.choice(quotes)
|
| 18 |
|
| 19 |
+
# Model setup
|
| 20 |
+
model = HfApiModel(
|
| 21 |
+
max_tokens=2096,
|
| 22 |
+
temperature=0.5,
|
| 23 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Load prompt templates
|
| 27 |
+
with open("prompts.yaml", 'r') as stream:
|
| 28 |
+
prompt_templates = yaml.safe_load(stream)
|
| 29 |
+
|
| 30 |
+
# Create the agent
|
| 31 |
+
agent = CodeAgent(
|
| 32 |
+
model=model,
|
| 33 |
+
tools=[get_motivational_quote], # Only the necessary tool
|
| 34 |
+
max_steps=3,
|
| 35 |
+
verbosity_level=1,
|
| 36 |
+
name="MotivationAgent",
|
| 37 |
+
description="An agent that provides motivational quotes to inspire users.",
|
| 38 |
+
prompt_templates=prompt_templates,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the Gradio UI
|
| 42 |
+
GradioUI(agent).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
|
|