NigelTaruvinga's picture
Upload app.py with huggingface_hub
ba29bc1 verified
import os
import requests
import gradio as gr
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
from crewai import Agent, Task, Crew, Process
os.environ["CEREBRAS_API_KEY"] = "csk-pk4m4ntefdvyt3vm495tre6jvenx9r88dytcyjt26mvv263m"
documents = [
"Apple Inc. is a multinational technology company headquartered in Cupertino, California. Key products include iPhone, Mac, iPad, Apple Watch, and Apple TV. Revenue in FY2024 was approximately $391 billion.",
"Microsoft Corporation is headquartered in Redmond, Washington. Key products include Windows, Azure, Office 365, and LinkedIn. Revenue in FY2024 was approximately $245 billion.",
"NVIDIA Corporation designs graphics processing units and AI chips. Key products include GeForce GPUs and CUDA platform. Revenue in FY2024 was approximately $60 billion.",
"Tesla Inc. designs and manufactures electric vehicles and clean energy products. Key models include Model S, Model 3, Model X, and Model Y.",
"Amazon.com Inc. focuses on e-commerce, cloud computing, and AI. AWS is the world leading cloud platform. Revenue in FY2024 was approximately $620 billion.",
"Google LLC specializes in internet services including search, advertising, cloud computing, and AI. Parent company is Alphabet Inc.",
"Meta Platforms operates Facebook, Instagram, WhatsApp, and Threads. Revenue in FY2024 was approximately $165 billion.",
"Anthropic is an AI safety company that builds Claude. Founded in 2021 by former OpenAI researchers.",
"OpenAI develops GPT models, ChatGPT, DALL-E, and Sora. Microsoft is a major investor.",
]
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=30)
docs = []
for text in documents:
for chunk in splitter.split_text(text):
docs.append(Document(page_content=chunk))
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vector_store = FAISS.from_documents(docs, embeddings)
def search_company_news(company_name):
try:
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{company_name.replace(' ', '_')}"
response = requests.get(url, timeout=10)
if response.status_code == 200:
data = response.json()
return f"Wikipedia: {data.get('extract', '')[:500]}"
return f"No data found for {company_name}"
except Exception as e:
return f"Search error: {str(e)}"
def retrieve_from_knowledge_base(company_name):
results = vector_store.similarity_search(company_name, k=3)
if results:
return "\n".join([r.page_content for r in results])
return f"No info found for {company_name}"
web_researcher = Agent(
role="Web Research Analyst",
goal="Gather latest information about a company from public sources",
backstory="You are an expert financial research analyst.",
llm="cerebras/llama3.1-8b",
verbose=False
)
kb_analyst = Agent(
role="Knowledge Base Analyst",
goal="Retrieve relevant company information from the internal knowledge base",
backstory="You are a specialist in querying structured knowledge bases.",
llm="cerebras/llama3.1-8b",
verbose=False
)
synthesiser = Agent(
role="Senior Investment Research Analyst",
goal="Synthesise information into a clear structured investment research report",
backstory="You are a senior analyst at a leading investment bank.",
llm="cerebras/llama3.1-8b",
verbose=False
)
def research_company(company_name):
if not company_name.strip():
return "Please enter a company name."
try:
web_task = Task(
description=f"Research {company_name}. Data: {search_company_news(company_name)[:400]}. Give 3 bullet points.",
expected_output="3 bullet points about the company",
agent=web_researcher
)
kb_task = Task(
description=f"Find info on {company_name}. Data: {retrieve_from_knowledge_base(company_name)[:400]}. Give 3 bullet points.",
expected_output="3 bullet points from knowledge base",
agent=kb_analyst
)
synth_task = Task(
description=f"Write a 200-word research report on {company_name} with sections: 1. Overview 2. Financials 3. Investment Considerations.",
expected_output="A 200-word structured research report",
agent=synthesiser,
context=[web_task, kb_task]
)
crew = Crew(
agents=[web_researcher, kb_analyst, synthesiser],
tasks=[web_task, kb_task, synth_task],
process=Process.sequential,
verbose=False
)
result = crew.kickoff()
return str(result)
except Exception as e:
return f"Error: {str(e)}"
app = gr.Interface(
fn=research_company,
inputs=gr.Textbox(lines=1, placeholder="Enter company name e.g. Apple, Microsoft...", label="Company Name"),
outputs=gr.Textbox(lines=20, label="Research Report"),
title="Multi-Agent Investment Research Assistant",
description="Three AI agents collaborate using CrewAI, LangChain, FAISS, and Llama 3.1 to produce structured investment research reports.",
examples=[["Apple"], ["Microsoft"], ["NVIDIA"]],
theme=gr.themes.Soft()
)
app.launch(server_name="0.0.0.0", server_port=7860, share=False)