File size: 3,837 Bytes
03ba989 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
"""
Example: Using the AI-SL API with both text and file inputs
This demonstrates how the Gradio interface can handle both text input
and file uploads, using whichever one is provided.
"""
from gradio_client import Client
import requests
def test_text_input():
"""
Example 1: Using text input
"""
print("=== Testing Text Input ===")
# Connect to your hosted app
client = Client("https://huggingface.co/spaces/your-username/your-space")
# Test with text input
text_input = "Hello world! This is a test of the text input functionality."
# Call the interface with text input
result = client.predict(
text_input, # Text input
None, # File input (None)
True, # Use R2 storage
api_name="/predict"
)
# Process results
json_data, video_url, download_html = result
print(f"Status: {json_data['status']}")
print(f"Video URL: {video_url}")
return video_url
def test_file_input():
"""
Example 2: Using file input
"""
print("=== Testing File Input ===")
# Connect to your hosted app
client = Client("https://huggingface.co/spaces/your-username/your-space")
# Test with file input
file_path = "example_document.txt"
# Call the interface with file input
result = client.predict(
"", # Text input (empty)
file_path, # File input
True, # Use R2 storage
api_name="/predict"
)
# Process results
json_data, video_url, download_html = result
print(f"Status: {json_data['status']}")
print(f"Video URL: {video_url}")
return video_url
def test_priority_logic():
"""
Example 3: Testing the priority logic
"""
print("=== Testing Priority Logic ===")
# Connect to your hosted app
client = Client("https://huggingface.co/spaces/your-username/your-space")
# Test with both inputs (text should take priority)
text_input = "This text should be processed instead of the file."
file_path = "example_document.txt"
# Call the interface with both inputs
result = client.predict(
text_input, # Text input
file_path, # File input
True, # Use R2 storage
api_name="/predict"
)
# Process results
json_data, video_url, download_html = result
print(f"Status: {json_data['status']}")
print(f"Gloss: {json_data['gloss']}")
print(f"Video URL: {video_url}")
return video_url
def download_video(video_url, output_path):
"""
Download a video from URL
"""
try:
response = requests.get(video_url, stream=True)
response.raise_for_status()
with open(output_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Video downloaded to: {output_path}")
return True
except Exception as e:
print(f"Error downloading video: {e}")
return False
def main():
"""
Run all examples
"""
print("AI-SL Dual Input Testing")
print("=" * 50)
# Test text input
text_video_url = test_text_input()
if text_video_url:
download_video(text_video_url, "text_input_video.mp4")
print("\n" + "-" * 50 + "\n")
# Test file input
file_video_url = test_file_input()
if file_video_url:
download_video(file_video_url, "file_input_video.mp4")
print("\n" + "-" * 50 + "\n")
# Test priority logic
priority_video_url = test_priority_logic()
if priority_video_url:
download_video(priority_video_url, "priority_test_video.mp4")
print("\n" + "=" * 50)
print("Testing complete!")
if __name__ == "__main__":
main() |