YKajima commited on
Commit
21d09ab
1 Parent(s): a6573bc

add form validation

Browse files
Files changed (2) hide show
  1. app.py +35 -14
  2. requirements.txt +2 -1
app.py CHANGED
@@ -3,14 +3,23 @@ import os
3
  import re
4
  import requests
5
 
6
- from bs4 import BeautifulSoup
7
  import gradio as gr
 
8
  from spiralfilm import FilmCore, FilmConfig
9
 
 
 
 
 
 
 
 
 
10
 
11
  def greet(name):
12
  return "こんにちは " + name + "さん!! \n僕はパスカルくんだよ。よろしくね"
13
 
 
14
  def extract_texts(input_str):
15
  pattern = r"msg='([^']*)'"
16
  matches = re.findall(pattern, input_str)
@@ -38,29 +47,39 @@ async def summarize(input_text: str, input_url: str):
38
  config=config
39
  ).run_async()
40
  if input_url:
41
- res = requests.get(input_url)
42
- soup = BeautifulSoup(res.text)
43
- url_content = soup.find('title').text + '\n' + soup.find('body').text
44
- _prompt = f"""
45
- 以下の文章を要約してください。
46
- {url_content}
47
- """
 
 
 
48
 
49
- res = await FilmCore(
50
  prompt=_prompt,
51
  system_prompt="あなたは優秀なライターです。",
52
  config=config
53
  ).run_async()
 
 
54
 
55
- return res
56
 
 
 
 
 
 
57
  else:
58
- return "テキストかURLを入力してください。"
59
-
60
 
61
 
62
  async def chat(input_text, input_url):
 
63
  summary = await summarize(input_text, input_url)
 
64
  endpoint = os.environ.get("TWINROOM_API_BASE")
65
  payload = {
66
  "content": summary
@@ -74,13 +93,15 @@ async def chat(input_text, input_url):
74
  result += f'\n{input_url}'
75
  return result
76
 
 
77
  with gr.Blocks() as iface:
78
  # UI
79
  with gr.Row():
80
  with gr.Column():
81
  input_text = gr.Textbox(label="テキスト")
82
  input_url = gr.Textbox(label="URL")
83
- chat_btn = gr.Button("Chat")
 
84
  with gr.Column():
85
  output_text = gr.Textbox(label="回答")
86
 
@@ -89,4 +110,4 @@ with gr.Blocks() as iface:
89
 
90
 
91
  if __name__ == "__main__":
92
- iface.launch(auth=("spiralai", "spiralai"), share=True)
 
3
  import re
4
  import requests
5
 
 
6
  import gradio as gr
7
+ from bs4 import BeautifulSoup
8
  from spiralfilm import FilmCore, FilmConfig
9
 
10
+ from logging import getLogger, DEBUG, StreamHandler
11
+
12
+ logger = getLogger()
13
+ logger.setLevel(DEBUG)
14
+ handler = StreamHandler()
15
+ handler.setLevel(DEBUG)
16
+ logger.addHandler(handler)
17
+
18
 
19
  def greet(name):
20
  return "こんにちは " + name + "さん!! \n僕はパスカルくんだよ。よろしくね"
21
 
22
+
23
  def extract_texts(input_str):
24
  pattern = r"msg='([^']*)'"
25
  matches = re.findall(pattern, input_str)
 
47
  config=config
48
  ).run_async()
49
  if input_url:
50
+ try:
51
+ res = requests.get(input_url)
52
+ soup = BeautifulSoup(res.text)
53
+ url_content = soup.find('title').text + '\n' + soup.find('body').text
54
+ _prompt = f"""
55
+ 以下の文章を要約してください。
56
+ {url_content}
57
+ """
58
+ except Exception:
59
+ raise gr.Error("WEBページの取得に失敗しました。")
60
 
61
+ return await FilmCore(
62
  prompt=_prompt,
63
  system_prompt="あなたは優秀なライターです。",
64
  config=config
65
  ).run_async()
66
+ else:
67
+ raise gr.Error("LLMの実行に失敗しました。")
68
 
 
69
 
70
+ def validate_input_form(input_text, input_url):
71
+ input_value = input_text + input_url
72
+ # Check if the text is not blank
73
+ if len(input_value) < 1:
74
+ raise gr.Error("テキストかURLを入力してください。")
75
  else:
76
+ return
 
77
 
78
 
79
  async def chat(input_text, input_url):
80
+ validate_input_form(input_text, input_url)
81
  summary = await summarize(input_text, input_url)
82
+ logger.info(f"summary: {summary}")
83
  endpoint = os.environ.get("TWINROOM_API_BASE")
84
  payload = {
85
  "content": summary
 
93
  result += f'\n{input_url}'
94
  return result
95
 
96
+
97
  with gr.Blocks() as iface:
98
  # UI
99
  with gr.Row():
100
  with gr.Column():
101
  input_text = gr.Textbox(label="テキスト")
102
  input_url = gr.Textbox(label="URL")
103
+
104
+ chat_btn = gr.Button("Chats")
105
  with gr.Column():
106
  output_text = gr.Textbox(label="回答")
107
 
 
110
 
111
 
112
  if __name__ == "__main__":
113
+ iface.launch(auth=("spiralai", "spiralai"), share=True, server_name="0.0.0.0")
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  SpiralFilm==0.2.5
2
- BeautifulSoup4==4.12.3
 
 
1
  SpiralFilm==0.2.5
2
+ BeautifulSoup4==4.12.3
3
+ gradio==4.25.0