DenverJones commited on
Commit
f774b33
·
verified ·
1 Parent(s): 323babf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -5,23 +5,17 @@ from langchain.agents import initialize_agent, Tool
5
  from langchain.prompts import PromptTemplate
6
  from langchain_huggingface import HuggingFaceEndpoint
7
 
8
- # Load environment variables (Make sure these are set in your Space Secrets)
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 if env var not set
11
 
12
- # ... (Rest of your code: prompts, tools, etc.)
 
 
 
 
13
 
14
- # Initialize LLM (Now HF_MODEL_ID is defined)
15
- llm = HuggingFaceEndpoint(
16
- repo_id=HF_MODEL_ID, # This will now work
17
- temperature=0.7,
18
- max_new_tokens=512,
19
- huggingfacehub_api_token=HF_API_KEY,
20
- task="text-generation",
21
- )
22
-
23
-
24
- # Define tools (motivational quote tool)
25
  def get_motivational_quote():
26
  quotes = [
27
  "Believe you can and you're halfway there.",
@@ -37,7 +31,7 @@ tools = [
37
  Tool(
38
  name="MotivationalQuote",
39
  func=get_motivational_quote,
40
- description="Use this tool to get a motivational quote.", # Clear description
41
  )
42
  ]
43
 
@@ -50,22 +44,21 @@ llm = HuggingFaceEndpoint(
50
  task="text-generation",
51
  )
52
 
53
- # Initialize Agent (using a suitable agent type)
54
  agent = initialize_agent(
55
  tools,
56
  llm,
57
- agent= "zero-shot-react-description", # or AgentType.ZERO_SHOT_REACT_DESCRIPTION
58
  verbose=True,
59
  prompt=prompt_template
60
  )
61
 
62
-
63
  def chat_with_agent(user_input, history):
64
  if not user_input.strip():
65
  return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
66
 
67
  try:
68
- bot_response = agent.run(user_input) # Or agent.invoke(user_input) or agent(user_input)
69
  except Exception as e:
70
  bot_response = f"Sorry, an error occurred: {e}"
71
 
@@ -73,7 +66,6 @@ def chat_with_agent(user_input, history):
73
  history.append({"role": "assistant", "content": bot_response})
74
  return history
75
 
76
-
77
  # Gradio UI
78
  with gr.Blocks() as demo:
79
  gr.Markdown("### Motivational Quote Agent")
 
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": "{{user_input}}"
15
+ }
16
+ prompt_template = PromptTemplate(template=prompts["default"], input_variables=["user_input"])
17
 
18
+ # Define tools
 
 
 
 
 
 
 
 
 
 
19
  def get_motivational_quote():
20
  quotes = [
21
  "Believe you can and you're halfway there.",
 
31
  Tool(
32
  name="MotivationalQuote",
33
  func=get_motivational_quote,
34
+ description="Use this tool to get a motivational quote.",
35
  )
36
  ]
37
 
 
44
  task="text-generation",
45
  )
46
 
47
+ # Initialize Agent
48
  agent = initialize_agent(
49
  tools,
50
  llm,
51
+ agent="zero-shot-react-description", # Or AgentType.ZERO_SHOT_REACT_DESCRIPTION
52
  verbose=True,
53
  prompt=prompt_template
54
  )
55
 
 
56
  def chat_with_agent(user_input, history):
57
  if not user_input.strip():
58
  return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
59
 
60
  try:
61
+ bot_response = agent.run(user_input) # Or agent.invoke(user_input) or agent(user_input)
62
  except Exception as e:
63
  bot_response = f"Sorry, an error occurred: {e}"
64
 
 
66
  history.append({"role": "assistant", "content": bot_response})
67
  return history
68
 
 
69
  # Gradio UI
70
  with gr.Blocks() as demo:
71
  gr.Markdown("### Motivational Quote Agent")