实现terminalcode和文件交互
This commit is contained in:
1 parent
287e1935f6
commit
a5e5aff63f
141 files changed
+2385
-1826
No files matched your search
+24
-7
@@ -12,6 +12,8 @@ import requests
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import base64
|
||||
import mimetypes
|
||||
|
||||
DEFAULT_URL = "http://127.0.0.1:8901/v1/chat/completions"
|
||||
|
||||
@@ -33,12 +35,24 @@ def get_audio_duration_sec(file_path: str) -> float:
|
||||
return -1.0
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
def file_to_url(file_path: str) -> str:
|
||||
"""Convert local file path to a file:// URL accepted by vLLM."""
|
||||
abs_path = os.path.abspath(file_path)
|
||||
return "file://" + abs_path.replace("\\", "/")
|
||||
def file_to_url(file_path: str, use_base64: bool = False) -> str:
|
||||
"""Convert local file path to a URL accepted by vLLM.
|
||||
|
||||
When the server is remote, use base64 data URL instead of file://
|
||||
since the remote server cannot access local filesystem paths.
|
||||
"""
|
||||
if use_base64:
|
||||
mime_type, _ = mimetypes.guess_type(file_path)
|
||||
if mime_type is None:
|
||||
mime_type = "audio/wav"
|
||||
with open(file_path, "rb") as f:
|
||||
b64_data = base64.b64encode(f.read()).decode("utf-8")
|
||||
return f"data:{mime_type};base64,{b64_data}"
|
||||
else:
|
||||
abs_path = os.path.abspath(file_path)
|
||||
return "file://" + abs_path.replace("\\", "/")
|
||||
|
||||
def caption_audio(audio_path: str, server_url: str = DEFAULT_URL) -> str:
|
||||
def caption_audio(audio_path: str, server_url: str = DEFAULT_URL, use_base64: bool = False) -> str:
|
||||
"""
|
||||
Send audio to the Qwen3-Omni-30B-A3B-Captioner and return its description.
|
||||
"""
|
||||
@@ -54,7 +68,7 @@ def caption_audio(audio_path: str, server_url: str = DEFAULT_URL) -> str:
|
||||
file=sys.stderr
|
||||
)
|
||||
|
||||
audio_url = file_to_url(audio_path)
|
||||
audio_url = file_to_url(audio_path, use_base64=use_base64)
|
||||
|
||||
# The model expects NO text prompt. Only the audio input is required.
|
||||
payload = {
|
||||
@@ -95,14 +109,17 @@ def main():
|
||||
help="Output text file (if not provided, print to stdout)")
|
||||
parser.add_argument("--verbose", "-v", action="store_true",
|
||||
help="Print detailed request/response info")
|
||||
parser.add_argument("--base64", "-b", action="store_true",
|
||||
help="Send audio as base64 data URL (use when server is remote)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.verbose:
|
||||
print(f"Audio file: {args.audio}")
|
||||
print(f"Server URL: {args.url}")
|
||||
print(f"Base64 mode: {args.base64}")
|
||||
|
||||
caption_text = caption_audio(args.audio, args.url)
|
||||
caption_text = caption_audio(args.audio, args.url, use_base64=args.base64)
|
||||
|
||||
if args.output:
|
||||
with open(args.output, "w", encoding="utf-8") as f:
|
||||
|
||||
Reference in new issue
Block a user