Spaces:
Sleeping
Sleeping
Dua Rajper commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import json
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# --- Functions (Simulated) ---
|
| 7 |
+
def get_current_weather(location):
|
| 8 |
+
"""Simulates fetching the current weather for a given location."""
|
| 9 |
+
weather_data = {
|
| 10 |
+
"location": location,
|
| 11 |
+
"temperature": "32°C", # Changed for consistency
|
| 12 |
+
"conditions": "Sunny", # Changed for consistency
|
| 13 |
+
"humidity": "50%" # Changed for consistency
|
| 14 |
+
}
|
| 15 |
+
return json.dumps(weather_data)
|
| 16 |
+
|
| 17 |
+
def search_wikipedia(query):
|
| 18 |
+
"""Simulates searching Wikipedia for a given query."""
|
| 19 |
+
search_results = {
|
| 20 |
+
"query": query,
|
| 21 |
+
"summary": f"This is a simulated summary for the query: {query}. Real information would be here."
|
| 22 |
+
}
|
| 23 |
+
return json.dumps(search_results)
|
| 24 |
+
|
| 25 |
+
available_functions = {
|
| 26 |
+
"get_current_weather": get_current_weather,
|
| 27 |
+
"search_wikipedia": search_wikipedia
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# --- Agent Interaction Function ---
|
| 31 |
+
model = pipeline("text-generation", model="gpt2") # Keep it outside the function
|
| 32 |
+
|
| 33 |
+
def run_agent(user_query):
|
| 34 |
+
prompt = f"""You are a helpful information-gathering agent.
|
| 35 |
+
Your goal is to answer user queries effectively. You have access to the following tools (functions):
|
| 36 |
+
{list(available_functions.keys())}.
|
| 37 |
+
|
| 38 |
+
For certain questions, you might need to use these tools to get the most up-to-date information.
|
| 39 |
+
When you decide to use a tool, respond in a JSON format specifying the 'action' (the function name)
|
| 40 |
+
and the 'parameters' for that function.
|
| 41 |
+
|
| 42 |
+
If you can answer the question directly without using a tool, respond in a JSON format with
|
| 43 |
+
'action': 'final_answer' and 'answer': 'your direct answer'.
|
| 44 |
+
|
| 45 |
+
Example 1:
|
| 46 |
+
User query: What's the weather like in Karachi?
|
| 47 |
+
Agent response: {{"action": "get_current_weather", "parameters": {{"location": "Karachi"}}}}
|
| 48 |
+
|
| 49 |
+
Example 2:
|
| 50 |
+
User query: Tell me about the capital of Pakistan.
|
| 51 |
+
Agent response: {{"action": "final_answer", "answer": "The capital of Pakistan is Islamabad."}}
|
| 52 |
+
|
| 53 |
+
Example 3:
|
| 54 |
+
User query: Search for information about the Mughal Empire.
|
| 55 |
+
Agent response: {{"action": "search_wikipedia", "parameters": {{"query": "Mughal Empire"}}}}
|
| 56 |
+
|
| 57 |
+
User query: {user_query}
|
| 58 |
+
Agent response: """
|
| 59 |
+
|
| 60 |
+
output = model(prompt, max_new_tokens=100, num_return_sequences=1, stop_sequence="\n")[0]['generated_text']
|
| 61 |
+
return output
|
| 62 |
+
|
| 63 |
+
# --- Response Processing Function ---
|
| 64 |
+
def process_agent_response(agent_response_json):
|
| 65 |
+
try:
|
| 66 |
+
response_data = json.loads(agent_response_json)
|
| 67 |
+
action = response_data.get("action")
|
| 68 |
+
|
| 69 |
+
if action == "get_current_weather":
|
| 70 |
+
parameters = response_data.get("parameters", {})
|
| 71 |
+
location = parameters.get("location")
|
| 72 |
+
if location:
|
| 73 |
+
weather = get_current_weather(location)
|
| 74 |
+
return {"final_answer": f"The agent called 'get_current_weather' for '{location}'. Simulated result: {weather}"}
|
| 75 |
+
else:
|
| 76 |
+
return {"final_answer": "Error: Location not provided for weather lookup."}
|
| 77 |
+
elif action == "search_wikipedia":
|
| 78 |
+
parameters = response_data.get("parameters", {})
|
| 79 |
+
query = parameters.get("query")
|
| 80 |
+
if query:
|
| 81 |
+
search_result = search_wikipedia(query)
|
| 82 |
+
return {"final_answer": f"The agent called 'search_wikipedia' for '{query}'. Simulated result: {search_result}"}
|
| 83 |
+
else:
|
| 84 |
+
return {"final_answer": "Error: Query not provided for Wikipedia search."}
|
| 85 |
+
elif action == "final_answer":
|
| 86 |
+
return {"final_answer": response_data.get("answer", "No direct answer provided.")}
|
| 87 |
+
else:
|
| 88 |
+
return {"final_answer": f"Unknown action: {action}"}
|
| 89 |
+
except json.JSONDecodeError:
|
| 90 |
+
return {"final_answer": "Error decoding agent's response."}
|
| 91 |
+
|
| 92 |
+
# --- Streamlit App ---
|
| 93 |
+
def main():
|
| 94 |
+
st.title("Gen AI Information Gathering Agent")
|
| 95 |
+
st.write("Ask me a question, and I'll try to answer it using my simulated tools!")
|
| 96 |
+
|
| 97 |
+
user_query = st.text_input("Your question:", "What's the weather like in London?")
|
| 98 |
+
if user_query:
|
| 99 |
+
agent_response = run_agent(user_query)
|
| 100 |
+
st.write(f"Agent Response (JSON):")
|
| 101 |
+
st.json(agent_response) # Use st.json for better display
|
| 102 |
+
|
| 103 |
+
processed_response = process_agent_response(agent_response)
|
| 104 |
+
st.write("Processed Response:")
|
| 105 |
+
st.write(processed_response["final_answer"])
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
main()
|