soiz1 commited on
Commit
bb47651
·
verified ·
1 Parent(s): 9a321f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -122
app.py CHANGED
@@ -337,125 +337,50 @@ def voice_conversion(source, target, diffusion_steps, length_adjust, inference_c
337
  yield mp3_bytes, None
338
 
339
 
340
- import gradio as gr
341
- from g4f.client import Client
342
- import markdown2 # より豊富なマークダウン対応
343
- import base64
344
- from io import BytesIO
345
- import json
346
-
347
- client = Client()
348
-
349
- def format_output(text):
350
- """
351
- チャットGPTスタイルのマークダウン形式に対応するためのフォーマット関数
352
- """
353
- return markdown2.markdown(text, extras=[
354
- "fenced-code-blocks",
355
- "tables",
356
- "task_list",
357
- "strike",
358
- "spoiler",
359
- "markdown-in-html"
360
- ])
361
-
362
- def image_to_data_url(image):
363
- """
364
- 画像をBase64形式のdataURLに変換する関数
365
- """
366
- buffered = BytesIO()
367
- image.save(buffered, format="PNG")
368
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
369
- return f"data:image/png;base64,{img_str}"
370
-
371
- def respond(message, history, system_message, max_tokens, temperature, top_p, model_choice, web_search, image=None):
372
- # システムメッセージを先頭に追加
373
- messages = [{"role": "system", "content": system_message}]
374
-
375
- # これまでの会話履歴を追加
376
- for user_msg, assistant_msg in history:
377
- if user_msg:
378
- messages.append({"role": "user", "content": user_msg})
379
- if assistant_msg:
380
- messages.append({"role": "assistant", "content": assistant_msg})
381
-
382
- # 今回のユーザーメッセージを追加
383
- if image:
384
- message += f"\n![image]({image})" # 画像をマークダウンで追加
385
- messages.append({"role": "user", "content": message})
386
-
387
- # API 呼び出し
388
- response = client.chat.completions.create(
389
- model=model_choice,
390
- messages=messages,
391
- max_tokens=max_tokens,
392
- temperature=temperature,
393
- top_p=top_p,
394
- web_search=web_search
395
- )
396
-
397
- formatted_response = format_output(response.choices[0].message.content)
398
- return formatted_response
399
-
400
- def chat(message, history, system_message, max_tokens, temperature, top_p, model_choice, web_search, image):
401
- if message.strip() == "" and not image:
402
- return "", history, history
403
- if image:
404
- image_data_url = image_to_data_url(image)
405
- else:
406
- image_data_url = None
407
-
408
- print("メッセージ送信直後の履歴:")
409
- print(json.dumps(history, ensure_ascii=False, indent=2)) # メッセージ送信前の履歴をJSONで表示
410
-
411
- response = respond(message, history, system_message, max_tokens, temperature, top_p, model_choice, web_search, image_data_url)
412
- history = history + [(message, response)]
413
-
414
- print("AIの回答直後の履歴:")
415
- print(json.dumps(history, ensure_ascii=False, indent=2)) # AIの回答後の履歴をJSONで表示
416
-
417
- # 入力欄をクリアして、更新済みのチャット履歴と状態を返す
418
- return "", history, history
419
-
420
- with gr.Blocks() as demo:
421
- with gr.Row():
422
- # 左側のカラム:入力欄と各オプションを配置
423
- with gr.Column():
424
- # オプション(システムメッセージや各種パラメータ)
425
- system_message = gr.Textbox(
426
- value="あなたは日本語しか話せません。あなたは最新の医療支援AIです。薬の紹介、薬の提案、薬の作成など、さまざまなタスクに答えます。また、新しい薬を開発する際は、既存のものに頼らずに画期的なアイデアを出します。",
427
- label="システムメッセージ"
428
- )
429
- max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="トークン制限")
430
- temperature = gr.Slider(minimum=0.1, maximum=4.0, value=2, step=0.1, label="Temperature (数値が大きいほど様々な回答をします。)")
431
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling) (数値が低いと回答候補が上位のみになります。)")
432
- model_choice = gr.Radio(choices=["gpt-4o-mini", "o3-mini"], value="gpt-4o-mini", label="モデル選択")
433
- web_search = gr.Checkbox(value=True, label="WEB検索")
434
-
435
- # チャット入力欄と送信ボタン
436
- chatbot_input = gr.Textbox(show_label=False, placeholder="ここにメッセージを入力してください...", lines=2)
437
- image_input = gr.Image(type="pil", label="画像をアップロード", visible=False) # 画像アップロード
438
- submit_btn = gr.Button("送信")
439
-
440
- # 右側のカラム:チャットの履歴を表示
441
- with gr.Column():
442
- chat_history_display = gr.Chatbot(label="チャット履歴")
443
-
444
- # 会話の状態(履歴)を保持する State コンポーネント
445
- state = gr.State([])
446
-
447
- # 送信ボタン押下時の挙動
448
- submit_btn.click(
449
- chat,
450
- inputs=[chatbot_input, state, system_message, max_tokens, temperature, top_p, model_choice, web_search, image_input],
451
- outputs=[chatbot_input, chat_history_display, state]
452
- )
453
-
454
- # エンターキーでの送信にも対応
455
- chatbot_input.submit(
456
- chat,
457
- inputs=[chatbot_input, state, system_message, max_tokens, temperature, top_p, model_choice, web_search, image_input],
458
- outputs=[chatbot_input, chat_history_display, state]
459
- )
460
-
461
- demo.launch()
 
337
  yield mp3_bytes, None
338
 
339
 
340
+ # 画像とMP3ファイルの情報
341
+ gallery_items = [
342
+ {"image": "default/sikokumetan.webp", "mp3": "default/sikokumetan.mp3"}
343
+ ]
344
+
345
+ # 画像をクリックした時に呼び出す関数
346
+ def set_reference_audio(image_path):
347
+ # ギャラリーの画像に対応するMP3ファイルを返す
348
+ for item in gallery_items:
349
+ if item["image"] == image_path:
350
+ return item["mp3"]
351
+ return None
352
+
353
+ if __name__ == "__main__":
354
+ description = ("Zero-shot音声変換モデル(学習不要)。ローカルでの利用方法は[GitHubリポジトリ](https://github.com/Plachtaa/seed-vc)をご覧ください。"
355
+ "参考音声が25秒を超える場合、自動的に25秒にクリップされます。"
356
+ "また、元音声と参考音声の合計時間が30秒を超える場合、元音声は分割処理されます。")
357
+
358
+ inputs = [
359
+ gr.Audio(type="filepath", label="元音声"),
360
+ gr.Audio(type="filepath", label="参考音声"),
361
+ gr.Slider(minimum=1, maximum=200, value=10, step=1, label="拡散ステップ数", info="デフォルトは10、50~100が最適な品質"),
362
+ gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="長さ調整", info="1.0未満で速度を上げ、1.0以上で速度を遅くします"),
363
+ gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.7, label="推論CFG率", info="わずかな影響があります"),
364
+ gr.Checkbox(label="F0条件付きモデルを使用", value=False, info="歌声変換には必須です"),
365
+ gr.Checkbox(label="F0自動調整", value=True, info="F0をおおよそ調整して目標音声に合わせます。F0条件付きモデル使用時にのみ有効です"),
366
+ gr.Slider(label='音程変換', minimum=-24, maximum=24, step=1, value=0, info="半音単位の音程変換。F0条件付きモデル使用時にのみ有効です"),
367
+ gr.Gallery(value=[item["image"] for item in gallery_items], label="画像ギャラリー", interactive=True)
368
+ ]
369
+
370
+ def update_reference_audio(selected_image):
371
+ # ギャラリーで選択された画像に対応するMP3ファイルを返す
372
+ mp3_file = set_reference_audio(selected_image)
373
+ return mp3_file
374
+
375
+ # ギャラリーで選択された画像を参考音声の入力に反映
376
+ gr.Interface(
377
+ fn=voice_conversion,
378
+ description=description,
379
+ inputs=inputs,
380
+ outputs=[gr.Audio(label="ストリーム出力音声", streaming=True, format='mp3'),
381
+ gr.Audio(label="完全出力音声", streaming=False, format='wav')],
382
+ title="Seed Voice Conversion",
383
+ examples=examples,
384
+ cache_examples=False,
385
+ live=True
386
+ ).launch()