chenw-ai21's picture
Update app.py
2b20c53 verified
import gradio as gr
from ai21 import AI21Client
from ai21.models.chat import ChatMessage, DocumentSchema
import json, requests, re
from bs4 import BeautifulSoup
import os
client = AI21Client(api_key=os.getenv('AI21_api_key'))
cik = "0000091142"
headers = {'User-Agent': 'AI21 Labs chenw@ai21.com'}
# latest 10Q
filing_lookup_url = f"https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK={cik}&type=10-Q&dateb=&owner=exclude&count=1"
response = requests.get(filing_lookup_url, headers=headers)
response.raise_for_status()
doc_link_match = re.search(r'<a href="(/Archives/edgar/data/[^"]+)"', response.text)
doc_link = "https://www.sec.gov" + doc_link_match.group(1)
response = requests.get(doc_link, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', class_='tableFile')
for row in table.find_all('tr'):
cells = row.find_all('td')
if len(cells) >= 4 and '10-Q' in cells[3].text:
doc_href = cells[2].a['href']
full_doc_url = f"https://www.sec.gov{doc_href}"
full_doc_url = full_doc_url.replace('ix?doc=/', '')
response = requests.get(full_doc_url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
full_text_10Q = ' '.join(soup.stripped_strings)
# latest 10K
filing_lookup_url = f"https://www.sec.gov/cgi-bin/browse-edgar?action=getcompany&CIK={cik}&type=10-K&dateb=&owner=exclude&count=1"
response = requests.get(filing_lookup_url, headers=headers)
response.raise_for_status()
doc_link_match = re.search(r'<a href="(/Archives/edgar/data/[^"]+)"', response.text)
doc_link = "https://www.sec.gov" + doc_link_match.group(1)
response = requests.get(doc_link, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', class_='tableFile')
for row in table.find_all('tr'):
cells = row.find_all('td')
if len(cells) >= 4 and '10-K' in cells[3].text:
doc_href = cells[2].a['href']
full_doc_url = f"https://www.sec.gov{doc_href}"
full_doc_url = full_doc_url.replace('ix?doc=/', '')
response = requests.get(full_doc_url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
full_text_10K = ' '.join(soup.stripped_strings)
AOS_10K = DocumentSchema(
content=full_text_10K,
metadata={'company': 'A.O. Smith Corporation', 'doc_type': 'SEC annual financial earnings report', 'year': '2023'}
)
AOS_10Q = DocumentSchema(
content=full_text_10Q,
metadata={'company': 'A.O. Smith Corporation', 'doc_type': 'SEC quarterly financial earnings report', 'year': '2024', 'quarter': "Q3", 'month': 'September'}
)
documents = [AOS_10K, AOS_10Q]
def jamba_chat(query):
system_message = "You are a helpful financial assistant expert. Please try to elaborate on your answer."
messages = [
ChatMessage(role="system", content=system_message),
ChatMessage(role="user", content=query)
]
chat_completions = client.chat.completions.create(
messages = messages,
model = "jamba-1.5-large",
documents = documents
)
return chat_completions.choices[0].message.content
demo = gr.Interface(
fn=jamba_chat,
inputs=[gr.Textbox(label="Your questions:", lines=2)],
outputs=[gr.Textbox(label="AI21 answer:", lines=2)],
examples=[
"What is the potential of A.O. Smith Corporation filing for bankruptcy within the next 3 years?",
"Summarize the financial performance of A.O. Smith Corporation over the last 2 years in a few bullet points",
"What are some key insights to understand the competitive landscape for A.O. Smith Corporation?",
"What is the level of risk associated with lending to A.O. Smith Corporation?",
"What is the exposure of A.O. Smith Corporation within its industry?",
"What is the main business risk of A.O. Smith Corporation?",
"What is the A.O. Smith Corporation financial performance?",
"What is the A.O. Smith Corporation liquidity?",
"What is the A.O. Smith Corporation core businesses and product portfolio?",
"What is the financial performance of A.O. Smith Corporation core businesses and product portfolio?",
"What are the A.O. Smith Corporation major events mentioned in the SEC reports?",
"How is A.O. Smith Corporation allocating its capital (e.g., dividends, share repurchases, acquisitions)?",
"Are there any concerning trends in operating cash flow for A.O. Smith Corporation?",
"What are all the A.O. Smith Corporation product brands?",
],
title="A.O. Smith Corporation Q&A",
description="Use AI21 Jamba model to answer question about A.O. Smith Corporation",
flagging_mode="never"
)
demo.launch(share=True, debug=False)