Skip to content

Commit

Permalink
Merge pull request #63 from boostcampaitech5/dev
Browse files Browse the repository at this point in the history
[Branch] Dev To Main
  • Loading branch information
dbsrlskfdk authored Aug 1, 2023
2 parents 1c6f203 + 82f6636 commit 53eda29
Show file tree
Hide file tree
Showing 29 changed files with 1,439 additions and 2,120 deletions.
2 changes: 1 addition & 1 deletion Backend/Sentimental/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if __name__ == "__main__":
import uvicorn

uvicorn.run("app.main:app", host="0.0.0.0", port=30007, reload=True)
uvicorn.run("Sentimental.main:app", host="0.0.0.0", port=30007, reload=True)
112 changes: 70 additions & 42 deletions Backend/Sentimental/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,94 @@

app = FastAPI()

MODEL_PATH = "/opt/ml/input/model-roberta_large-sota_trainer"
tokenizer = AutoTokenizer.from_pretrained("klue/roberta-large")
MODEL_PATH = "/opt/ml/outputs/klue/roberta-large_merged-4_100-26_100"

model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
tokenizer = AutoTokenizer.from_pretrained("klue/roberta-large")
special_tokens_dict = {'additional_special_tokens': ['[COMPANY]','[/COMPANY]']}
num_added_toks = tokenizer.add_special_tokens(special_tokens_dict)

@app.get("/")
def hello_world():
return {"hello": "world"}
model.resize_token_embeddings(len(tokenizer))

device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = model.to(device)

def predict_sentiment(text):
model.eval()
with torch.no_grad() :
temp = tokenizer(
text,
return_tensors='pt',
padding=True,
truncation=True,
##
max_length=100,
# stride=stride,
# return_overflowing_tokens=True,
return_offsets_mapping=False
)


predicted_label = model(input_ids=temp['input_ids'],
token_type_ids=temp['token_type_ids'])

print(predicted_label)

results = []
results = torch.nn.Softmax(dim=-1)(predicted_label.logits)
def extract_sentences_token(input_dict, pad_token_id):
'''
512 토큰 초과라면 input_ids, token_type_ids, attention_maks를
앞 128, 뒤 384으로 분리합니다.
이하라면 뒤는 pad 토큰으로 채웁니다.
사용 방법:
train_encoding = tokenizer(text)
train_encoding = extract_sentences_token(train_encoding, tokenizer.pad_token_id)
'''
new = {}
batch_size = len(input_dict['input_ids'])
new['input_ids'] = torch.ones(batch_size, 512, dtype=int)
new['token_type_ids'] = torch.ones(batch_size, 512, dtype=int)
new['attention_mask'] = torch.ones(batch_size, 512, dtype=int)
# batch_size, 512
for i in range(batch_size):
a = input_dict['input_ids'][i]
a = a[a != pad_token_id]
length = len(a)
if length > 512:
left, right = 1, 3
a = torch.cat((a[:128*left], a[-128*right:]), dim=0)
new['input_ids'][i] = a
new['token_type_ids'][i] = input_dict['token_type_ids'][i][:512]
new['attention_mask'][i] = input_dict['attention_mask'][i][:512]
else:
new['input_ids'][i] = input_dict['input_ids'][i][:512]
new['token_type_ids'][i] = input_dict['token_type_ids'][i][:512]
new['attention_mask'][i] = input_dict['attention_mask'][i][:512]
return new

def predict_sentiment(text: List[str]) -> List[str]:
answer = []
print(results)
for result in results :
if result[0]>=result[1] :
answer.append("부정")

else :
answer.append("긍정")

model.eval()

loader = torch.utils.data.DataLoader(dataset=text, batch_size=32, shuffle=False)
with torch.no_grad() :
for batch_text in loader:
temp = tokenizer(
batch_text,
return_tensors='pt',
padding="max_length",
truncation=True,
max_length=3000, # 충분히 커서 모두 토큰화할 길이
)
temp = extract_sentences_token(temp, tokenizer.pad_token_id)
if torch.cuda.is_available():
temp = {key: value.to(device) for key, value in temp.items()}
predicted_label = model(**temp)

results = torch.nn.Softmax(dim=-1)(predicted_label.logits)
for result in results :
if result[0]>=result[1] :
answer.append("부정")
else :
answer.append("긍정")

return answer

class FinanaceSentiment(BaseModel):
corpus_list: list = []
title: str = "title"
company: str = "삼성전자"
result: Optional[List]
company_list: list = []


@app.post("/classify_sentiment", description="문장의 감정을 분류합니다.")
async def classify_sentiment(finance: FinanaceSentiment):
# 입력으로 받은 텍스트를 모델로 예측합니다.
predictions = predict_sentiment(finance.corpus_list)
input = []
for corpus, company in zip(finance.corpus_list, finance.company_list) :
input.append(f"이 기사는[COMPANY]{company}[/COMPANY]에 대한 기사야. {corpus}")

predictions = predict_sentiment(input)

# 결과를 반환합니다.
result = {
"title": finance.title,
# "input_text": finance.corpus,
"sentiment": predictions
}

Expand Down
2 changes: 1 addition & 1 deletion Frontend/deguel/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
<title>뉴키주 - 뉴스 키워드 주식</title>
</head>
<body>
<div id="root"></div>
Expand Down
32 changes: 4 additions & 28 deletions Frontend/deguel/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@

import {
Title,
Text,
Tab,
TabList,
TabGroup,
TabPanel,
TabPanels,
Grid,
Col,
Card,
Metric,
} from "@tremor/react";

import TreemMap from "./elements/TreeMap";
import SummaryCard from "./elements/Summary";

export default function App() {

return (
<main className="px-12 py-12 2xl:mx-32">
<Title>Deguel</Title>
<Text>현재, 언론사에서 가장 많이 다루고 있는 키워드 입니다. </Text>
<Metric>뉴키주</Metric>
<Title>현재, 언론사에서 가장 많이 다루고 있는 키워드 입니다. </Title>

<TabGroup className="mt-6">
<TabList>
Expand All @@ -42,27 +39,6 @@ export default function App() {
</TabPanel>
</TabPanels>
</TabGroup>

<Title className="mt-6">사용자 추천 기사</Title>
<Text>많은 사용자가 본 뉴스에요. </Text>

<Grid numItemsLg={2} className="gap-6 mt-6">
<Col numColSpanLg={1}>
<Card>
<SummaryCard isMain={true}>
<div className="h-96" />
</SummaryCard>
</Card>
</Col>

<Col numColSpanLg={1}>
<Card>
<SummaryCard isMain={true}>
<div className="h-96" />
</SummaryCard>
</Card>
</Col>
</Grid>
</main>
);
}
21 changes: 18 additions & 3 deletions Frontend/deguel/src/elements/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ import {

import { PropTypes } from 'prop-types';
import { useEffect, useState } from "react";
import { startOfYear, subDays } from "date-fns";

import { supabase } from "../supabaseClient";

import { startOfYear, subDays } from "date-fns";


// 원화 표기를 위한 함수.
const dataFormatter = (number) => `${Intl.NumberFormat("ko-KR", {
style: 'currency',
currency: 'KRW',
}).format(number).toString()}`;


export default function LineChartTab(props) {
const [selectedIndex, setSelectedIndex] = useState("Max");
const [selectedIndex, setSelectedIndex] = useState("4");
const [stockPrice, setStockPrice] = useState([{"id": 0, "ticker": "005930", "price": 0, "date": "2021-04-05"}, {"id": 0, "ticker": "005930", "price": 0, "date": "2021-04-05"}]);
const [ticker_name, setTickerName] = useState("a");

Expand All @@ -33,6 +37,7 @@ export default function LineChartTab(props) {
getInformations();
}, []);

// LineChartTab의 prop 값 설정.
LineChartTab.propTypes = {
ticker: PropTypes.string.isRequired,
}
Expand All @@ -41,6 +46,11 @@ export default function LineChartTab(props) {
async function getInformations() {
// DB에서 ticker에 맞는 가격 데이터 가져오기.
var { data } = await supabase.from("price").select("*").eq("ticker", props.ticker).order('date', { ascending: true });

for(let i = 0;i < data.length;i++) {
data[i].date = data[i].date.split("T")[0].slice();
}

setStockPrice(data);

// ticker에 맞는 주가 명 가져오기.
Expand Down Expand Up @@ -101,7 +111,7 @@ export default function LineChartTab(props) {
</BadgeDelta>
</Flex>
<Metric>{dataFormatter(stockPrice[stockPrice.length - 1].price)}</Metric>
<Text>최근 1주일간의 주식 차트에요!</Text>
<Text>선택하신 기간 동안의 주식 차트에요!</Text>
<TabGroup index={selectedIndex} onIndexChange={setSelectedIndex} className="mt-10">
<TabList variant="line">
<Tab>1M</Tab>
Expand All @@ -121,6 +131,7 @@ export default function LineChartTab(props) {
valueFormatter={dataFormatter}
showLegend={false}
yAxisWidth={48}
autoMinValue={true}
/>
</TabPanel>
<TabPanel>
Expand All @@ -133,6 +144,7 @@ export default function LineChartTab(props) {
valueFormatter={dataFormatter}
showLegend={false}
yAxisWidth={48}
autoMinValue={true}
/>
</TabPanel>
<TabPanel>
Expand All @@ -145,6 +157,7 @@ export default function LineChartTab(props) {
valueFormatter={dataFormatter}
showLegend={false}
yAxisWidth={48}
autoMinValue={true}
/>
</TabPanel>
<TabPanel>
Expand All @@ -157,6 +170,7 @@ export default function LineChartTab(props) {
valueFormatter={dataFormatter}
showLegend={false}
yAxisWidth={48}
autoMinValue={true}
/>
</TabPanel>
<TabPanel>
Expand All @@ -169,6 +183,7 @@ export default function LineChartTab(props) {
valueFormatter={dataFormatter}
showLegend={false}
yAxisWidth={48}
autoMinValue={true}
/>
</TabPanel>
</TabPanels>
Expand Down
Loading

0 comments on commit 53eda29

Please sign in to comment.