Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,51 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# LangChain community imports (Updated for v0.2+)
|
| 7 |
-
from langchain_community.document_loaders import TextLoader, PyPDFLoader, CSVLoader
|
| 8 |
-
from langchain_community.embeddings import OpenAIEmbeddings
|
| 9 |
-
from langchain_community.vectorstores import FAISS
|
| 10 |
-
from langchain_community.llms import OpenAI
|
| 11 |
-
from langchain_community.tools import DuckDuckGoSearchRun
|
| 12 |
-
|
| 13 |
-
# Other imports (you may have these depending on use)
|
| 14 |
-
from langchain.chains import RetrievalQA
|
| 15 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 16 |
-
from langchain.schema import Document
|
| 17 |
-
|
| 18 |
-
# Add your environment key for OpenAI if required
|
| 19 |
-
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY", "your-api-key-here")
|
| 20 |
-
|
| 21 |
-
def load_documents(directory: str):
|
| 22 |
-
"""Loads documents from a directory using supported loaders."""
|
| 23 |
-
docs = []
|
| 24 |
-
for filename in os.listdir(directory):
|
| 25 |
-
filepath = os.path.join(directory, filename)
|
| 26 |
-
if filename.endswith(".txt"):
|
| 27 |
-
loader = TextLoader(filepath)
|
| 28 |
-
elif filename.endswith(".pdf"):
|
| 29 |
-
loader = PyPDFLoader(filepath)
|
| 30 |
-
elif filename.endswith(".csv"):
|
| 31 |
-
loader = CSVLoader(filepath)
|
| 32 |
-
else:
|
| 33 |
-
continue
|
| 34 |
-
docs.extend(loader.load())
|
| 35 |
-
return docs
|
| 36 |
-
|
| 37 |
-
def build_vector_store(docs):
|
| 38 |
-
"""Build FAISS index from documents using OpenAI embeddings."""
|
| 39 |
-
embeddings = OpenAIEmbeddings()
|
| 40 |
-
return FAISS.from_documents(docs, embeddings)
|
| 41 |
-
|
| 42 |
-
def build_qa_chain(vectorstore):
|
| 43 |
-
"""Create a RetrievalQA chain from the vector store."""
|
| 44 |
-
retriever = vectorstore.as_retriever()
|
| 45 |
-
llm = OpenAI(temperature=0)
|
| 46 |
-
return RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
|
| 47 |
-
|
| 48 |
-
def main():
|
| 49 |
-
# Load and process data
|
| 50 |
-
data_path = "data/" # Change to your actual directory
|
| 51 |
-
print("[INFO] Loading documents...")
|
| 52 |
-
documents = load_documents(data_path)
|
| 53 |
-
|
| 54 |
-
print("[INFO] Splitting text...")
|
| 55 |
-
splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 56 |
-
split_docs = splitter.split_documents(documents)
|
| 57 |
-
|
| 58 |
-
print("[INFO] Creating vector store...")
|
| 59 |
-
vectorstore = build_vector_store(split_docs)
|
| 60 |
-
|
| 61 |
-
print("[INFO] Building QA chain...")
|
| 62 |
-
qa_chain = build_qa_chain(vectorstore)
|
| 63 |
-
|
| 64 |
-
print("\n[READY] Ask questions (type 'exit' to quit):\n")
|
| 65 |
-
while True:
|
| 66 |
-
question = input("Q: ")
|
| 67 |
-
if question.lower() in ["exit", "quit"]:
|
| 68 |
-
print("Goodbye!")
|
| 69 |
-
break
|
| 70 |
-
answer = qa_chain.run(question)
|
| 71 |
-
print("A:", answer)
|
| 72 |
-
|
| 73 |
-
# Main entry point
|
| 74 |
if __name__ == "__main__":
|
| 75 |
-
|
| 76 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from api import get_all_questions, submit_answers, get_file
|
| 3 |
+
from agent import answer_question
|
| 4 |
+
from tools.file_loader import read_pdf, read_csv, read_txt
|
| 5 |
+
|
| 6 |
+
qa_data = []
|
| 7 |
+
answers = []
|
| 8 |
+
|
| 9 |
+
def run_agent():
|
| 10 |
+
global qa_data, answers
|
| 11 |
+
qa_data = get_all_questions()
|
| 12 |
+
answers = []
|
| 13 |
+
for q in qa_data:
|
| 14 |
+
task_id = q['task_id']
|
| 15 |
+
question = q['question']
|
| 16 |
+
file_text = None
|
| 17 |
+
if q.get("files"):
|
| 18 |
+
for fname in q["files"]:
|
| 19 |
+
raw = get_file(task_id, fname)
|
| 20 |
+
if fname.endswith(".pdf"):
|
| 21 |
+
file_text = read_pdf(raw)
|
| 22 |
+
elif fname.endswith(".csv"):
|
| 23 |
+
file_text = read_csv(raw)
|
| 24 |
+
elif fname.endswith(".txt"):
|
| 25 |
+
file_text = read_txt(raw)
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
response = answer_question(question, file_context=file_text, do_search=True)
|
| 29 |
+
answers.append({"task_id": task_id, "submitted_answer": response})
|
| 30 |
+
return f"Agent answered {len(answers)} questions. Ready to submit!"
|
| 31 |
+
|
| 32 |
+
def handle_submit(username, code_link):
|
| 33 |
+
return submit_answers(username, code_link, answers)
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("# GAIA Level 1 Agent")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
username = gr.Textbox(label="Hugging Face Username")
|
| 40 |
+
code_link = gr.Textbox(label="Space Code URL (/tree/main)")
|
| 41 |
+
|
| 42 |
+
status = gr.Textbox(label="Status", lines=4)
|
| 43 |
+
|
| 44 |
+
run_btn = gr.Button(" Run Agent")
|
| 45 |
+
submit_btn = gr.Button("Submit Answers")
|
| 46 |
+
|
| 47 |
+
run_btn.click(fn=run_agent, outputs=status)
|
| 48 |
+
submit_btn.click(fn=handle_submit, inputs=[username, code_link], outputs=status)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|
|
|