DenverJones commited on
Commit
958ed2e
·
verified ·
1 Parent(s): d36b03e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -43
app.py CHANGED
@@ -1,12 +1,20 @@
1
- from smolagents import CodeAgent, HfApiModel, tool
2
  import yaml
3
  import gradio as gr
4
- import requests
 
 
5
 
6
- # Define a tool to generate motivational quotes
7
- @tool
8
- def get_motivational_quote() -> str:
9
- """Returns a motivational quote to inspire the user."""
 
 
 
 
 
 
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
- # Model setup
21
- API_URL = "https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud"
22
- HEADERS = {"Authorization": f"Bearer YOUR_HF_API_KEY"}
 
 
 
 
23
 
24
- def query(payload):
25
- response = requests.post(API_URL, headers=HEADERS, json=payload)
26
- return response.json()
 
 
 
27
 
28
- model = HfApiModel(
29
- max_tokens=512, # Reduced max_tokens for stability
30
- temperature=0.7,
31
- model_id=API_URL
32
  )
33
 
34
- # Load prompt templates
35
- with open("prompts.yaml", 'r') as stream:
36
- prompt_templates = yaml.safe_load(stream)
37
-
38
- # Create the agent
39
- agent = CodeAgent(
40
- model=model,
41
- tools=[get_motivational_quote], # Only the necessary tool
42
- max_steps=3,
43
- verbosity_level=1,
44
- name="MotivationAgent",
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
- # Send request to Hugging Face API
55
- response = query({"inputs": user_input, "parameters": {"max_tokens": 100}})
56
- bot_response = response.get("generated_text", "Sorry, I couldn't generate a response.")
57
-
58
- # Ensure history is in the correct format
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()