Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,30 @@
|
|
| 1 |
import os
|
| 2 |
import yaml
|
| 3 |
import gradio as gr
|
| 4 |
-
from langchain.agents import Tool
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
-
import json # For parsing JSON responses (if needed)
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
HF_API_KEY = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 11 |
-
HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "google/flan-t5-xxl")
|
| 12 |
|
| 13 |
-
# Load prompt templates
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
def get_motivational_quote():
|
| 20 |
quotes = [
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
]
|
| 23 |
import random
|
| 24 |
return random.choice(quotes)
|
|
@@ -27,84 +33,49 @@ tools = [
|
|
| 27 |
Tool(
|
| 28 |
name="MotivationalQuote",
|
| 29 |
func=get_motivational_quote,
|
| 30 |
-
description="Use this tool to get a motivational quote.
|
| 31 |
)
|
| 32 |
]
|
| 33 |
|
| 34 |
-
# Initialize LLM
|
| 35 |
llm = HuggingFaceEndpoint(
|
| 36 |
repo_id=HF_MODEL_ID,
|
| 37 |
temperature=0.7,
|
| 38 |
max_new_tokens=512,
|
| 39 |
huggingfacehub_api_token=HF_API_KEY,
|
| 40 |
-
task="text-generation"
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
facts = "" # Initialize facts
|
| 52 |
-
plan = planning_prompt.format(task=user_input, tools=tools, answer_facts=facts)
|
| 53 |
-
print("Initial Plan:", plan)
|
| 54 |
-
|
| 55 |
-
# Example (Very simplified - you MUST expand this)
|
| 56 |
-
llm_response = llm(system_prompt.format(tools=tools) + plan) # Call LLM with system and plan prompts
|
| 57 |
-
print("LLM Response:", llm_response)
|
| 58 |
-
|
| 59 |
-
# *** PARSE LLM RESPONSE ***
|
| 60 |
-
# This is where you need to extract the next action (tool call or direct answer)
|
| 61 |
-
# from the LLM's response. You'll likely need a specific format in the LLM's output.
|
| 62 |
-
# Example (assuming the LLM outputs JSON):
|
| 63 |
-
try:
|
| 64 |
-
response_json = json.loads(llm_response) # This will only work if the LLM response is valid JSON
|
| 65 |
-
action = response_json.get("action")
|
| 66 |
-
action_input = response_json.get("action_input")
|
| 67 |
-
|
| 68 |
-
if action == "MotivationalQuote":
|
| 69 |
-
quote = get_motivational_quote()
|
| 70 |
-
return quote
|
| 71 |
-
# ... handle other actions (tool calls or direct answers)
|
| 72 |
-
elif action == "final_answer":
|
| 73 |
-
return action_input
|
| 74 |
-
else:
|
| 75 |
-
return "I don't know how to do that yet."
|
| 76 |
-
|
| 77 |
-
except json.JSONDecodeError:
|
| 78 |
-
return "Invalid LLM Response. I don't know how to do that yet."
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# *** UPDATE FACTS AND PLAN ***
|
| 82 |
-
# After each action, update the facts and create a new plan if needed.
|
| 83 |
-
# This will depend on the LLM's response and the results of tool calls.
|
| 84 |
-
# ...
|
| 85 |
-
|
| 86 |
-
# Example (replace with your actual logic)
|
| 87 |
-
return "Agent response (replace with actual agent output)"
|
| 88 |
|
| 89 |
|
| 90 |
def chat_with_agent(user_input, history):
|
| 91 |
if not user_input.strip():
|
| 92 |
return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
history.append({"role": "user", "content": user_input})
|
| 96 |
history.append({"role": "assistant", "content": bot_response})
|
| 97 |
return history
|
| 98 |
|
| 99 |
|
| 100 |
-
# Gradio UI
|
| 101 |
-
with gr.Blocks() as demo:
|
| 102 |
-
gr.Markdown("###
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
user_input = gr.Textbox(placeholder="Ask me for motivation... (e.g., 'I need inspiration')")
|
| 106 |
-
|
| 107 |
submit_btn = gr.Button("Send")
|
| 108 |
submit_btn.click(chat_with_agent, inputs=[user_input, chatbot], outputs=chatbot)
|
| 109 |
|
| 110 |
-
demo.launch()
|
|
|
|
| 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") # Or any other suitable model
|
| 11 |
|
| 12 |
+
# Load prompt templates (simplified - just the default)
|
| 13 |
+
prompts = {
|
| 14 |
+
"default": |
|
| 15 |
+
{{user_input}}
|
| 16 |
+
}
|
| 17 |
+
prompt_template = PromptTemplate(template=prompts["default"], input_variables=["user_input"])
|
| 18 |
|
| 19 |
+
|
| 20 |
+
# Define tools (motivational quote tool)
|
| 21 |
def get_motivational_quote():
|
| 22 |
quotes = [
|
| 23 |
+
"Believe you can and you're halfway there.",
|
| 24 |
+
"Your limitation—it's only your imagination.",
|
| 25 |
+
"Push yourself, because no one else is going to do it for you.",
|
| 26 |
+
"Great things never come from comfort zones.",
|
| 27 |
+
"Dream it. Wish it. Do it.",
|
| 28 |
]
|
| 29 |
import random
|
| 30 |
return random.choice(quotes)
|
|
|
|
| 33 |
Tool(
|
| 34 |
name="MotivationalQuote",
|
| 35 |
func=get_motivational_quote,
|
| 36 |
+
description="Use this tool to get a motivational quote.", # Clear description
|
| 37 |
)
|
| 38 |
]
|
| 39 |
|
| 40 |
+
# Initialize LLM
|
| 41 |
llm = HuggingFaceEndpoint(
|
| 42 |
repo_id=HF_MODEL_ID,
|
| 43 |
temperature=0.7,
|
| 44 |
max_new_tokens=512,
|
| 45 |
huggingfacehub_api_token=HF_API_KEY,
|
| 46 |
+
task="text-generation",
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Initialize Agent (using a suitable agent type)
|
| 50 |
+
agent = initialize_agent(
|
| 51 |
+
tools,
|
| 52 |
+
llm,
|
| 53 |
+
agent= "zero-shot-react-description", # or AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
| 54 |
+
verbose=True,
|
| 55 |
+
prompt=prompt_template
|
| 56 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
def chat_with_agent(user_input, history):
|
| 60 |
if not user_input.strip():
|
| 61 |
return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
|
| 62 |
|
| 63 |
+
try:
|
| 64 |
+
bot_response = agent.run(user_input) # Or agent.invoke(user_input) or agent(user_input)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
bot_response = f"Sorry, an error occurred: {e}"
|
| 67 |
+
|
| 68 |
history.append({"role": "user", "content": user_input})
|
| 69 |
history.append({"role": "assistant", "content": bot_response})
|
| 70 |
return history
|
| 71 |
|
| 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()
|