ssboost commited on
Commit
c0ed664
ยท
verified ยท
1 Parent(s): 8eaeb94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -1,38 +1,48 @@
1
  import pandas as pd
2
- import openpyxl
3
- from openpyxl.chart import BarChart, Reference, PieChart
4
- from openpyxl.chart.label import DataLabelList
5
- from openpyxl.utils.dataframe import dataframe_to_rows
6
- from datetime import datetime
7
  from io import BytesIO
8
- import matplotlib.pyplot as plt
9
  import gradio as gr
 
10
  import tempfile
 
11
 
12
- # Read excel data for review analysis
13
- def read_excel_data(file):
14
- df = pd.read_excel(BytesIO(file), usecols="B, C, D, E", skiprows=1, names=["Review Date", "Option", "Review", "ReviewScore"])
15
- df['Review Date'] = pd.to_datetime(df['Review Date']).dt.tz_localize(None).dt.date
16
- df['Year-Month'] = df['Review Date'].astype(str).str.slice(0, 7)
17
- df['Year'] = df['Review Date'].astype(str).str.slice(0, 4)
18
- df['Month'] = df['Review Date'].astype(str).str.slice(5, 7)
19
- df['Day'] = df['Review Date'].astype(str).str.slice(8, 10)
20
- df['Option1'] = df['Option'].str.split(" / ").str[0] # 1์ฐจ ์˜ต์…˜๋งŒ ์ถ”์ถœ
21
- df['Review Length'] = df['Review'].str.len() # ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„: ๋ฆฌ๋ทฐ ๊ธธ์ด ๊ณ„์‚ฐ
22
- return df
23
 
24
- def extract_longest_reviews(df):
25
- longest_reviews = df.groupby('ReviewScore').apply(lambda x: x.nlargest(100, 'Review Length', keep='all')).reset_index(drop=True)
26
- return longest_reviews.drop(columns=['Review Length', 'Year-Month', 'Year', 'Month', 'Day', 'Option1', 'Option2']) # ์‚ญ์ œ๋œ ์—ด๋“ค
 
 
 
 
 
 
 
 
27
 
28
  # ๊ธด ๊ธ€ ํ…์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜ ์ •์˜
29
  def long_text_result(file):
30
- df = read_excel_data(file)
31
- longest_reviews = extract_longest_reviews(df)
32
- text_result = longest_reviews.to_string(index=False)
33
- return text_result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- # ์ƒˆ๋กœ์šด ํƒญ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
36
  def chatbot_response(input_text):
37
  response = f"์ฑ—๋ด‡ ์‘๋‹ต: {input_text}"
38
  return response
 
1
  import pandas as pd
 
 
 
 
 
2
  from io import BytesIO
 
3
  import gradio as gr
4
+ from gradio_client import Client
5
  import tempfile
6
+ import os
7
 
8
+ # API ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
9
+ client = Client("https://ssboost-excel-ra-vector-db-test1.hf.space/--replicas/q77r3/")
 
 
 
 
 
 
 
 
 
10
 
11
+ # ์—‘์…€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ํ•จ์ˆ˜ (API ์‚ฌ์šฉ์œผ๋กœ ํ•„์š” ์—†์–ด์ง)
12
+ # def read_excel_data(file):
13
+ # df = pd.read_excel(BytesIO(file), usecols="B, C, D, E", skiprows=1, names=["Review Date", "Option", "Review", "ReviewScore"])
14
+ # df['Review Date'] = pd.to_datetime(df['Review Date']).dt.tz_localize(None).dt.date
15
+ # df['Year-Month'] = df['Review Date'].astype(str).str.slice(0, 7)
16
+ # df['Year'] = df['Review Date'].astype(str).str.slice(0, 4)
17
+ # df['Month'] = df['Review Date'].astype(str).str.slice(5, 7)
18
+ # df['Day'] = df['Review Date'].astype(str).str.slice(8, 10)
19
+ # df['Option1'] = df['Option'].str.split(" / ").str[0] # 1์ฐจ ์˜ต์…˜๋งŒ ์ถ”์ถœ
20
+ # df['Review Length'] = df['Review'].str.len() # ์ถ”๊ฐ€๋œ ๋ถ€๋ถ„: ๋ฆฌ๋ทฐ ๊ธธ์ด ๊ณ„์‚ฐ
21
+ # return df
22
 
23
  # ๊ธด ๊ธ€ ํ…์ŠคํŠธ ๊ฒฐ๊ณผ๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜ ์ •์˜
24
  def long_text_result(file):
25
+ # ์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ์ €์žฅ
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp_file:
27
+ tmp_file.write(file)
28
+ tmp_file_path = tmp_file.name
29
+
30
+ # API ํ˜ธ์ถœ์„ ํ†ตํ•ด ๋ถ„์„, ์ €์žฅ, ๋ฒกํ„ฐ DB ๋ฐ ์ธ๋ฑ์‹ฑ ์ง„ํ–‰
31
+ result = client.predict(
32
+ tmp_file_path,
33
+ 100,
34
+ 10,
35
+ api_name="/analyze_and_initialize_db"
36
+ )
37
+
38
+ # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
39
+ os.remove(tmp_file_path)
40
+
41
+ # ๋ถ„์„ ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ
42
+ analysis = "์ƒํ’ˆ์˜ ์žฅ๋‹จ์  10๊ฐ€์ง€ ๋ถ„์„ ๊ฒฐ๊ณผ:\n" + str(result)
43
+ return analysis
44
 
45
+ # ์ฑ—๋ด‡ ์‘๋‹ต ์ƒ์„ฑ
46
  def chatbot_response(input_text):
47
  response = f"์ฑ—๋ด‡ ์‘๋‹ต: {input_text}"
48
  return response