Upload 3 files
#31
by
Rashmi0801
- opened
- README.md +7 -13
- app.py +54 -196
- requirements.txt +63 -2
README.md
CHANGED
|
@@ -1,15 +1,9 @@
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
pinned: false
|
| 10 |
-
hf_oauth: true
|
| 11 |
-
# optional, default duration is 8 hours/480 minutes. Max duration is 30 days/43200 minutes.
|
| 12 |
-
hf_oauth_expiration_minutes: 480
|
| 13 |
---
|
| 14 |
-
|
| 15 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
title: Search Engine
|
| 4 |
+
sdk: streamlit
|
| 5 |
+
emoji: π»
|
| 6 |
+
colorFrom: red
|
| 7 |
+
colorTo: yellow
|
| 8 |
+
short_description: Search Engine with LLM
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,196 +1,54 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
response.raise_for_status()
|
| 56 |
-
questions_data = response.json()
|
| 57 |
-
if not questions_data:
|
| 58 |
-
print("Fetched questions list is empty.")
|
| 59 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 60 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 61 |
-
except requests.exceptions.RequestException as e:
|
| 62 |
-
print(f"Error fetching questions: {e}")
|
| 63 |
-
return f"Error fetching questions: {e}", None
|
| 64 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 65 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 66 |
-
print(f"Response text: {response.text[:500]}")
|
| 67 |
-
return f"Error decoding server response for questions: {e}", None
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 71 |
-
|
| 72 |
-
# 3. Run your Agent
|
| 73 |
-
results_log = []
|
| 74 |
-
answers_payload = []
|
| 75 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 76 |
-
for item in questions_data:
|
| 77 |
-
task_id = item.get("task_id")
|
| 78 |
-
question_text = item.get("question")
|
| 79 |
-
if not task_id or question_text is None:
|
| 80 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
-
continue
|
| 82 |
-
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
-
except Exception as e:
|
| 87 |
-
print(f"Error running agent on task {task_id}: {e}")
|
| 88 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 89 |
-
|
| 90 |
-
if not answers_payload:
|
| 91 |
-
print("Agent did not produce any answers to submit.")
|
| 92 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
-
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
-
print(status_update)
|
| 98 |
-
|
| 99 |
-
# 5. Submit
|
| 100 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 101 |
-
try:
|
| 102 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 103 |
-
response.raise_for_status()
|
| 104 |
-
result_data = response.json()
|
| 105 |
-
final_status = (
|
| 106 |
-
f"Submission Successful!\n"
|
| 107 |
-
f"User: {result_data.get('username')}\n"
|
| 108 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 109 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 110 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 111 |
-
)
|
| 112 |
-
print("Submission successful.")
|
| 113 |
-
results_df = pd.DataFrame(results_log)
|
| 114 |
-
return final_status, results_df
|
| 115 |
-
except requests.exceptions.HTTPError as e:
|
| 116 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 117 |
-
try:
|
| 118 |
-
error_json = e.response.json()
|
| 119 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 120 |
-
except requests.exceptions.JSONDecodeError:
|
| 121 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 122 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 123 |
-
print(status_message)
|
| 124 |
-
results_df = pd.DataFrame(results_log)
|
| 125 |
-
return status_message, results_df
|
| 126 |
-
except requests.exceptions.Timeout:
|
| 127 |
-
status_message = "Submission Failed: The request timed out."
|
| 128 |
-
print(status_message)
|
| 129 |
-
results_df = pd.DataFrame(results_log)
|
| 130 |
-
return status_message, results_df
|
| 131 |
-
except requests.exceptions.RequestException as e:
|
| 132 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 133 |
-
print(status_message)
|
| 134 |
-
results_df = pd.DataFrame(results_log)
|
| 135 |
-
return status_message, results_df
|
| 136 |
-
except Exception as e:
|
| 137 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
| 138 |
-
print(status_message)
|
| 139 |
-
results_df = pd.DataFrame(results_log)
|
| 140 |
-
return status_message, results_df
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# --- Build Gradio Interface using Blocks ---
|
| 144 |
-
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 146 |
-
gr.Markdown(
|
| 147 |
-
"""
|
| 148 |
-
**Instructions:**
|
| 149 |
-
|
| 150 |
-
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
-
---
|
| 155 |
-
**Disclaimers:**
|
| 156 |
-
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 157 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 158 |
-
"""
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
gr.LoginButton()
|
| 162 |
-
|
| 163 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
-
|
| 165 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 166 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 167 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 168 |
-
|
| 169 |
-
run_button.click(
|
| 170 |
-
fn=run_and_submit_all,
|
| 171 |
-
outputs=[status_output, results_table]
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
if __name__ == "__main__":
|
| 175 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 176 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 177 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 178 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 179 |
-
|
| 180 |
-
if space_host_startup:
|
| 181 |
-
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 182 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 183 |
-
else:
|
| 184 |
-
print("βΉοΈ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
-
|
| 186 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 187 |
-
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 188 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 189 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 190 |
-
else:
|
| 191 |
-
print("βΉοΈ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
-
|
| 193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
-
|
| 195 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_groq import ChatGroq
|
| 3 |
+
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 4 |
+
from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
|
| 5 |
+
from langchain.agents import initialize_agent, AgentType
|
| 6 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Used the inbuilt tools of Arxiv and Wikipedia
|
| 11 |
+
api_wrapper_arxiv = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=250)
|
| 12 |
+
arxiv = ArxivQueryRun(api_wrapper=api_wrapper_arxiv)
|
| 13 |
+
|
| 14 |
+
api_wrapper_wiki = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=250)
|
| 15 |
+
wiki = WikipediaQueryRun(api_wrapper=api_wrapper_wiki)
|
| 16 |
+
|
| 17 |
+
search = DuckDuckGoSearchRun(name="Search")
|
| 18 |
+
|
| 19 |
+
st.title("Langchain - Chat with Search")
|
| 20 |
+
"""
|
| 21 |
+
In this example, we're using `StreamlitCallbackHandler` to display the thoughts and actions of an agent in an interactive Streamlit app.
|
| 22 |
+
Try more LangChain π€ Streamlit Agent examples at [github.com/langchain-ai/streamlit-agent](https://github.com/langchain-ai/streamlit-agent).
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
# Sidebar for settings
|
| 26 |
+
st.sidebar.title("Settings")
|
| 27 |
+
api_key = st.sidebar.text_input("Enter your Groq API Key:", type="password")
|
| 28 |
+
|
| 29 |
+
if "messages" not in st.session_state:
|
| 30 |
+
st.session_state["messages"] = [
|
| 31 |
+
{"role":"assistant", "content":"Hi, I am a Chatbot who can search the web. How can I help you ?"}
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
for msg in st.session_state.messages:
|
| 35 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 36 |
+
|
| 37 |
+
if prompt:=st.chat_input(placeholder="What is machine learning ?"):
|
| 38 |
+
st.session_state.messages.append({"role":"user", "content":prompt})
|
| 39 |
+
st.chat_message("user").write(prompt)
|
| 40 |
+
|
| 41 |
+
llm = ChatGroq(groq_api_key=api_key, model_name="Llama3-8b-8192", streaming=True)
|
| 42 |
+
tools = [search, arxiv, wiki]
|
| 43 |
+
|
| 44 |
+
search_agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, handle_parsing_errors=True)
|
| 45 |
+
|
| 46 |
+
with st.chat_message("assistant"):
|
| 47 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 48 |
+
response = search_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 49 |
+
st.session_state.messages.append({'role':'assistant', "content":response})
|
| 50 |
+
st.write(response)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,2 +1,63 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
ipykernel
|
| 3 |
+
python-dotenv
|
| 4 |
+
langchain_community
|
| 5 |
+
langchain-community
|
| 6 |
+
pypdf
|
| 7 |
+
bs4
|
| 8 |
+
beautifulsoup4
|
| 9 |
+
arxiv
|
| 10 |
+
pymupdf
|
| 11 |
+
wikipedia
|
| 12 |
+
langchain-text-splitters
|
| 13 |
+
langchain-openai
|
| 14 |
+
chromadb
|
| 15 |
+
sentence_transformers
|
| 16 |
+
langchain_huggingface
|
| 17 |
+
faiss-cpu
|
| 18 |
+
langchain_chroma
|
| 19 |
+
langchain-chroma
|
| 20 |
+
streamlit
|
| 21 |
+
langchain-ollama
|
| 22 |
+
langchain_groq
|
| 23 |
+
langchain_core
|
| 24 |
+
uvicorn
|
| 25 |
+
fastapi
|
| 26 |
+
pydantic
|
| 27 |
+
langchain
|
| 28 |
+
langserve
|
| 29 |
+
uvicorn
|
| 30 |
+
sse_starlette
|
| 31 |
+
packaging
|
| 32 |
+
langchain-groq
|
| 33 |
+
pandas
|
| 34 |
+
duckdb
|
| 35 |
+
httpx
|
| 36 |
+
openai
|
| 37 |
+
duckduckgo-search
|
| 38 |
+
langchain-google-genai
|
| 39 |
+
google-generativeai
|
| 40 |
+
mysql-connector-python
|
| 41 |
+
SQLAlchemy
|
| 42 |
+
validators==0.28.1
|
| 43 |
+
youtube_transcript_api
|
| 44 |
+
pytube
|
| 45 |
+
unstructured
|
| 46 |
+
numexpr
|
| 47 |
+
huggingface_hub
|
| 48 |
+
cassio
|
| 49 |
+
datasets
|
| 50 |
+
tiktoken
|
| 51 |
+
PyPDF2
|
| 52 |
+
gradio
|
| 53 |
+
boto3
|
| 54 |
+
botocore
|
| 55 |
+
awscli
|
| 56 |
+
langchain_nvidia_ai_endpoints
|
| 57 |
+
crewai
|
| 58 |
+
crewai_tools
|
| 59 |
+
typing-extensions
|
| 60 |
+
pinecone
|
| 61 |
+
pinecone-client
|
| 62 |
+
pinecone-text
|
| 63 |
+
pinecone-notebooks
|