Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain_core.documents import Document
|
| 9 |
+
from crewai import Agent, Task, Crew, Process
|
| 10 |
+
|
| 11 |
+
os.environ["CEREBRAS_API_KEY"] = "csk-pk4m4ntefdvyt3vm495tre6jvenx9r88dytcyjt26mvv263m"
|
| 12 |
+
|
| 13 |
+
documents = [
|
| 14 |
+
"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.",
|
| 15 |
+
"Microsoft Corporation is headquartered in Redmond, Washington. Key products include Windows, Azure, Office 365, and LinkedIn. Revenue in FY2024 was approximately $245 billion.",
|
| 16 |
+
"NVIDIA Corporation designs graphics processing units and AI chips. Key products include GeForce GPUs and CUDA platform. Revenue in FY2024 was approximately $60 billion.",
|
| 17 |
+
"Tesla Inc. designs and manufactures electric vehicles and clean energy products. Key models include Model S, Model 3, Model X, and Model Y.",
|
| 18 |
+
"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.",
|
| 19 |
+
"Google LLC specializes in internet services including search, advertising, cloud computing, and AI. Parent company is Alphabet Inc.",
|
| 20 |
+
"Meta Platforms operates Facebook, Instagram, WhatsApp, and Threads. Revenue in FY2024 was approximately $165 billion.",
|
| 21 |
+
"Anthropic is an AI safety company that builds Claude. Founded in 2021 by former OpenAI researchers.",
|
| 22 |
+
"OpenAI develops GPT models, ChatGPT, DALL-E, and Sora. Microsoft is a major investor.",
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=30)
|
| 26 |
+
docs = []
|
| 27 |
+
for text in documents:
|
| 28 |
+
for chunk in splitter.split_text(text):
|
| 29 |
+
docs.append(Document(page_content=chunk))
|
| 30 |
+
|
| 31 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 32 |
+
vector_store = FAISS.from_documents(docs, embeddings)
|
| 33 |
+
|
| 34 |
+
def search_company_news(company_name):
|
| 35 |
+
try:
|
| 36 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{company_name.replace(' ', '_')}"
|
| 37 |
+
response = requests.get(url, timeout=10)
|
| 38 |
+
if response.status_code == 200:
|
| 39 |
+
data = response.json()
|
| 40 |
+
return f"Wikipedia: {data.get('extract', '')[:500]}"
|
| 41 |
+
return f"No data found for {company_name}"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Search error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
def retrieve_from_knowledge_base(company_name):
|
| 46 |
+
results = vector_store.similarity_search(company_name, k=3)
|
| 47 |
+
if results:
|
| 48 |
+
return "\n".join([r.page_content for r in results])
|
| 49 |
+
return f"No info found for {company_name}"
|
| 50 |
+
|
| 51 |
+
web_researcher = Agent(
|
| 52 |
+
role="Web Research Analyst",
|
| 53 |
+
goal="Gather latest information about a company from public sources",
|
| 54 |
+
backstory="You are an expert financial research analyst.",
|
| 55 |
+
llm="cerebras/llama3.1-8b",
|
| 56 |
+
verbose=False
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
kb_analyst = Agent(
|
| 60 |
+
role="Knowledge Base Analyst",
|
| 61 |
+
goal="Retrieve relevant company information from the internal knowledge base",
|
| 62 |
+
backstory="You are a specialist in querying structured knowledge bases.",
|
| 63 |
+
llm="cerebras/llama3.1-8b",
|
| 64 |
+
verbose=False
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
synthesiser = Agent(
|
| 68 |
+
role="Senior Investment Research Analyst",
|
| 69 |
+
goal="Synthesise information into a clear structured investment research report",
|
| 70 |
+
backstory="You are a senior analyst at a leading investment bank.",
|
| 71 |
+
llm="cerebras/llama3.1-8b",
|
| 72 |
+
verbose=False
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def research_company(company_name):
|
| 76 |
+
if not company_name.strip():
|
| 77 |
+
return "Please enter a company name."
|
| 78 |
+
try:
|
| 79 |
+
web_task = Task(
|
| 80 |
+
description=f"Research {company_name}. Data: {search_company_news(company_name)[:400]}. Give 3 bullet points.",
|
| 81 |
+
expected_output="3 bullet points about the company",
|
| 82 |
+
agent=web_researcher
|
| 83 |
+
)
|
| 84 |
+
kb_task = Task(
|
| 85 |
+
description=f"Find info on {company_name}. Data: {retrieve_from_knowledge_base(company_name)[:400]}. Give 3 bullet points.",
|
| 86 |
+
expected_output="3 bullet points from knowledge base",
|
| 87 |
+
agent=kb_analyst
|
| 88 |
+
)
|
| 89 |
+
synth_task = Task(
|
| 90 |
+
description=f"Write a 200-word research report on {company_name} with sections: 1. Overview 2. Financials 3. Investment Considerations.",
|
| 91 |
+
expected_output="A 200-word structured research report",
|
| 92 |
+
agent=synthesiser,
|
| 93 |
+
context=[web_task, kb_task]
|
| 94 |
+
)
|
| 95 |
+
crew = Crew(
|
| 96 |
+
agents=[web_researcher, kb_analyst, synthesiser],
|
| 97 |
+
tasks=[web_task, kb_task, synth_task],
|
| 98 |
+
process=Process.sequential,
|
| 99 |
+
verbose=False
|
| 100 |
+
)
|
| 101 |
+
result = crew.kickoff()
|
| 102 |
+
return str(result)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"Error: {str(e)}"
|
| 105 |
+
|
| 106 |
+
app = gr.Interface(
|
| 107 |
+
fn=research_company,
|
| 108 |
+
inputs=gr.Textbox(lines=1, placeholder="Enter company name e.g. Apple, Microsoft...", label="Company Name"),
|
| 109 |
+
outputs=gr.Textbox(lines=20, label="Research Report"),
|
| 110 |
+
title="Multi-Agent Investment Research Assistant",
|
| 111 |
+
description="Three AI agents collaborate using CrewAI, LangChain, FAISS, and Llama 3.1 to produce structured investment research reports.",
|
| 112 |
+
examples=[["Apple"], ["Microsoft"], ["NVIDIA"]],
|
| 113 |
+
theme=gr.themes.Soft()
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=False)
|