youngtsai commited on
Commit
2a3dc52
·
1 Parent(s): b3a02b4

render dynamic

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +47 -12
  3. requirements.txt +1 -1
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 📚
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.36.0
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: red
5
  colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 4.37.2
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -4,9 +4,18 @@ from openai import OpenAI
4
  import json
5
  import tempfile
6
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
-
9
- OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
10
  OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
11
 
12
 
@@ -28,12 +37,18 @@ def generate_topics(model, max_tokens, sys_content, scenario, eng_level, user_ge
28
  "model": model,
29
  "messages": messages,
30
  "max_tokens": max_tokens,
 
31
  }
32
 
33
  response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
34
- content = response.choices[0].message.content.strip()
 
 
35
 
36
- return content
 
 
 
37
 
38
  def generate_points(model, max_tokens, sys_content, scenario, eng_level, topic, user_generate_points_prompt):
39
  """
@@ -287,7 +302,11 @@ def paragraph_save_and_tts(paragraph_text):
287
  # Handle the error appropriately (e.g., return an error message or a default audio path)
288
  return paragraph_text, None
289
 
290
- with gr.Blocks() as demo:
 
 
 
 
291
  with gr.Row():
292
  with gr.Column():
293
  # basic inputs
@@ -303,12 +322,31 @@ with gr.Blocks() as demo:
303
  Give me 10 topics relevant to Scenario,
304
  for a paragraph. Just the topics, no explanation, use simple English language.
305
  Make sure the vocabulary you use is at english level.
 
 
 
 
306
  """
307
  user_generate_topics_prompt = gr.Textbox(label="Topics Prompt", value=default_generate_topics_prompt, visible=False)
308
- generate_topics_button = gr.Button("AI Generate Topic Sentences")
309
- topic_output = gr.Textbox(label="AI Generated Topic 主題")
310
- topic_input = gr.Textbox(label="Topic")
311
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
  gr.Markdown("## 3. Generate Points 要點")
313
  default_generate_points_prompt = """
314
  Please provide main points to develop in a paragraph about topic in the context of scenario,
@@ -487,9 +525,6 @@ with gr.Blocks() as demo:
487
  paragraph_save_output = gr.Textbox(label="Save and Share")
488
  audio_output = gr.Audio(label="Generated Speech", type="filepath")
489
 
490
-
491
-
492
-
493
  generate_topics_button.click(
494
  fn=generate_topics,
495
  inputs=[
 
4
  import json
5
  import tempfile
6
 
7
+ is_env_local = os.getenv("IS_ENV_LOCAL", "false") == "true"
8
+ print(f"is_env_local: {is_env_local}")
9
+
10
+ # KEY CONFIG
11
+ if is_env_local:
12
+ with open("local_config.json") as f:
13
+ config = json.load(f)
14
+ IS_ENV_PROD = "False"
15
+ OPEN_AI_KEY = config["OPEN_AI_KEY"]
16
+ else:
17
+ OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
18
 
 
 
19
  OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
20
 
21
 
 
37
  "model": model,
38
  "messages": messages,
39
  "max_tokens": max_tokens,
40
+ "response_format": { "type": "json_object" }
41
  }
42
 
43
  response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
44
+ content = response.choices[0].message.content
45
+ topics = json.loads(content)["topics"]
46
+ topics_text = json.dumps(topics)
47
 
48
+ return topics_text
49
+
50
+ def update_topic_input(topic):
51
+ return topic, gr.update(elem_classes=["btn-max-length", "btn-highlight"])
52
 
53
  def generate_points(model, max_tokens, sys_content, scenario, eng_level, topic, user_generate_points_prompt):
54
  """
 
302
  # Handle the error appropriately (e.g., return an error message or a default audio path)
303
  return paragraph_text, None
304
 
305
+ CSS = """
306
+ .btn-max-length { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
307
+ .btn-highlight { border-color: yellow; }
308
+ """
309
+ with gr.Blocks(css=CSS) as demo:
310
  with gr.Row():
311
  with gr.Column():
312
  # basic inputs
 
322
  Give me 10 topics relevant to Scenario,
323
  for a paragraph. Just the topics, no explanation, use simple English language.
324
  Make sure the vocabulary you use is at english level.
325
+ output use JSON
326
+
327
+ EXAMPLE:
328
+ "topics":["topic1", "topic2", "topic3", "topic4", "topic5", "topic6", "topic7", "topic8", "topic9", "topic10"]
329
  """
330
  user_generate_topics_prompt = gr.Textbox(label="Topics Prompt", value=default_generate_topics_prompt, visible=False)
331
+
332
+ with gr.Row():
333
+ with gr.Column():
334
+ topic_input = gr.Textbox(label="Topic")
335
+
336
+ with gr.Column():
337
+ generate_topics_button = gr.Button("AI Generate Topic Sentences")
338
+ topic_output = gr.Textbox(label="AI Generated Topic 主題", visible=False, value=[])
339
+
340
+ @gr.render(inputs=topic_output)
341
+ def render_topics(topics):
342
+ buttons = []
343
+ topics_list = json.loads(topics)
344
+ for i, topic in enumerate(topics_list):
345
+ btn = gr.Button(topic, elem_id=f"topic_button_{i}", elem_classes=["btn-max-length"])
346
+ btn.click(fn=update_topic_input, inputs=gr.Textbox(value=topic, visible=False), outputs=[topic_input, btn])
347
+ buttons.append(btn)
348
+ return buttons
349
+
350
  gr.Markdown("## 3. Generate Points 要點")
351
  default_generate_points_prompt = """
352
  Please provide main points to develop in a paragraph about topic in the context of scenario,
 
525
  paragraph_save_output = gr.Textbox(label="Save and Share")
526
  audio_output = gr.Audio(label="Generated Speech", type="filepath")
527
 
 
 
 
528
  generate_topics_button.click(
529
  fn=generate_topics,
530
  inputs=[
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- gradio==4.36.0
2
  openai>=1.16.2
 
1
+ gradio==4.37.2
2
  openai>=1.16.2