DenverJones commited on
Commit
88fb741
·
verified ·
1 Parent(s): c422538

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -66
app.py CHANGED
@@ -1,81 +1,44 @@
1
- import os
2
  import yaml
3
- import gradio as gr
4
- from langchain.agents import initialize_agent, Tool
5
- from langchain.prompts import PromptTemplate
6
- from langchain_huggingface import HuggingFaceEndpoint
7
 
8
- # Load environment variables
9
- HF_API_KEY = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
10
- HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "google/flan-t5-xxl") # Default model
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
- tools = [
35
- Tool(
36
- name="MotivationalQuote",
37
- func=get_motivational_quote,
38
- description="Use this tool to get a motivational quote.",
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
- # Initialize Agent
52
- agent = initialize_agent(
53
- tools,
54
- llm,
55
- agent="zero-shot-react-description", # Or AgentType.ZERO_SHOT_REACT_DESCRIPTION
56
- verbose=True,
57
- prompt=prompt_template
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(user_input) # Corrected: agent() or agent.invoke()
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
- # 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