mirror of
https://github.com/gradio-app/gradio.git
synced 2025-03-01 11:45:36 +08:00
Merge pull request #345 from gradio-app/abidlabs/tests-analytics-fix
Abidlabs/tests analytics fix
This commit is contained in:
commit
eaa9699283
@ -72,7 +72,7 @@ class Interface:
|
||||
title=None, description=None, article=None, thumbnail=None,
|
||||
css=None, server_port=None, server_name=networking.LOCALHOST_NAME, height=500, width=900,
|
||||
allow_screenshot=True, allow_flagging=True, flagging_options=None, encrypt=False,
|
||||
show_tips=False, flagging_dir="flagged", analytics_enabled=True, enable_queue=False, api_mode=False):
|
||||
show_tips=False, flagging_dir="flagged", analytics_enabled=None, enable_queue=False, api_mode=False):
|
||||
"""
|
||||
Parameters:
|
||||
fn (Callable): the function to wrap an interface around.
|
||||
@ -171,12 +171,20 @@ class Interface:
|
||||
self.server_port = server_port
|
||||
self.simple_server = None
|
||||
self.allow_screenshot = allow_screenshot
|
||||
self.allow_flagging = os.getenv("GRADIO_FLAGGING") or allow_flagging
|
||||
# If parameter is provided, use that; otherwise, environmet variable; otherwise, True
|
||||
if allow_flagging is None:
|
||||
self.allow_flagging = bool(os.getenv("GRADIO_FLAGGING") or True)
|
||||
else:
|
||||
self.allow_flagging = allow_flagging
|
||||
self.flagging_options = flagging_options
|
||||
self.flagging_dir = flagging_dir
|
||||
self.encrypt = encrypt
|
||||
Interface.instances.add(self)
|
||||
self.analytics_enabled = analytics_enabled
|
||||
# If parameter is provided, use that; otherwise, environmet variable; otherwise, True
|
||||
if analytics_enabled is None:
|
||||
self.analytics_enabled = bool(os.getenv("GRADIO_ANALYTICS") or True)
|
||||
else:
|
||||
self.analytics_enabled = analytics_enabled
|
||||
self.save_to = None
|
||||
self.share = None
|
||||
self.share_url = None
|
||||
|
@ -19,6 +19,9 @@ TIMEOUT = 10
|
||||
|
||||
GAP_TO_SCREENSHOT = 2
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
def wait_for_url(url):
|
||||
for i in range(TIMEOUT):
|
||||
try:
|
||||
|
@ -1,11 +1,15 @@
|
||||
import unittest
|
||||
import pathlib
|
||||
import gradio as gr
|
||||
import os
|
||||
|
||||
"""
|
||||
WARNING: These tests have an external dependency: namely that Hugging Face's Hub and Space APIs do not change, and they keep their most famous models up. So if, e.g. Spaces is down, then these test will not pass.
|
||||
"""
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class TestHuggingFaceModelAPI(unittest.TestCase):
|
||||
def test_question_answering(self):
|
||||
model_type = "question-answering"
|
||||
@ -146,25 +150,25 @@ class TestLoadInterface(unittest.TestCase):
|
||||
|
||||
def test_sentiment_model(self):
|
||||
interface_info = gr.external.load_interface("models/distilbert-base-uncased-finetuned-sst-2-english", alias="sentiment_classifier")
|
||||
io = gr.Interface(**interface_info)
|
||||
io = gr.Interface(**interface_info, analytics_enabled=False)
|
||||
output = io("I am happy, I love you.")
|
||||
self.assertGreater(output['Positive'], 0.5)
|
||||
|
||||
def test_image_classification_model(self):
|
||||
interface_info = gr.external.load_interface("models/google/vit-base-patch16-224")
|
||||
io = gr.Interface(**interface_info)
|
||||
io = gr.Interface(**interface_info, analytics_enabled=False)
|
||||
output = io("test/test_data/lion.jpg")
|
||||
self.assertGreater(output['lion'], 0.5)
|
||||
|
||||
def test_translation_model(self):
|
||||
interface_info = gr.external.load_interface("models/t5-base")
|
||||
io = gr.Interface(**interface_info)
|
||||
io = gr.Interface(**interface_info, analytics_enabled=False)
|
||||
output = io("My name is Sarah and I live in London")
|
||||
self.assertEquals(output, 'Mein Name ist Sarah und ich lebe in London')
|
||||
|
||||
def test_numerical_to_label_space(self):
|
||||
interface_info = gr.external.load_interface("spaces/abidlabs/titanic-survival")
|
||||
io = gr.Interface(**interface_info)
|
||||
io = gr.Interface(**interface_info, analytics_enabled=False)
|
||||
output = io("male", 77, 10)
|
||||
self.assertLess(output['Survives'], 0.5)
|
||||
|
||||
@ -174,7 +178,7 @@ class TestLoadInterface(unittest.TestCase):
|
||||
raise AssertionError("File does not exist: %s" % str(path))
|
||||
|
||||
interface_info = gr.external.load_interface("spaces/abidlabs/image-identity")
|
||||
io = gr.Interface(**interface_info)
|
||||
io = gr.Interface(**interface_info, analytics_enabled=False)
|
||||
output = io("test/test_data/lion.jpg")
|
||||
assertIsFile(output)
|
||||
|
||||
|
@ -8,7 +8,9 @@ from pydub import AudioSegment
|
||||
import os
|
||||
import tempfile
|
||||
import json
|
||||
import shutil
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class InputComponent(unittest.TestCase):
|
||||
@ -55,10 +57,10 @@ class TestTextbox(unittest.TestCase):
|
||||
self.assertIsInstance(text_input.generate_sample(), str)
|
||||
|
||||
def test_in_interface(self):
|
||||
iface = gr.Interface(lambda x: x[::-1], "textbox", "textbox")
|
||||
iface = gr.Interface(lambda x: x[::-1], "textbox", "textbox", analytics_enabled=False)
|
||||
self.assertEqual(iface.process(["Hello"])[0], ["olleH"])
|
||||
iface = gr.Interface(lambda sentence: max([len(word) for word in sentence.split()]), gr.inputs.Textbox(),
|
||||
gr.outputs.Textbox(), interpretation="default")
|
||||
gr.outputs.Textbox(), interpretation="default", analytics_enabled=False)
|
||||
scores, alternative_outputs = iface.interpret(["Return the length of the longest word in this sentence"])
|
||||
self.assertEqual(scores, [[('Return', 0.0), (' ', 0), ('the', 0.0), (' ', 0), ('length', 0.0), (' ', 0),
|
||||
('of', 0.0), (' ', 0), ('the', 0.0), (' ', 0), ('longest', 0.0), (' ', 0),
|
||||
@ -85,9 +87,9 @@ class TestNumber(unittest.TestCase):
|
||||
self.assertEqual(numeric_input.get_interpretation_neighbors(1), ([0.97, 0.98, 0.99, 1.01, 1.02, 1.03], {}))
|
||||
|
||||
def test_in_interface(self):
|
||||
iface = gr.Interface(lambda x: x**2, "number", "textbox")
|
||||
iface = gr.Interface(lambda x: x**2, "number", "textbox", analytics_enabled=False)
|
||||
self.assertEqual(iface.process([2])[0], ['4.0'])
|
||||
iface = gr.Interface(lambda x: x**2, "number", "textbox", interpretation="default")
|
||||
iface = gr.Interface(lambda x: x**2, "number", "textbox", interpretation="default", analytics_enabled=False)
|
||||
scores, alternative_outputs = iface.interpret([2])
|
||||
self.assertEqual(scores, [[(1.94, -0.23640000000000017), (1.96, -0.15840000000000032),
|
||||
(1.98, -0.07960000000000012), [2, None], (2.02, 0.08040000000000003),
|
||||
@ -120,9 +122,9 @@ class TestSlider(unittest.TestCase):
|
||||
})
|
||||
|
||||
def test_in_interface(self):
|
||||
iface = gr.Interface(lambda x: x**2, "slider", "textbox")
|
||||
iface = gr.Interface(lambda x: x**2, "slider", "textbox", analytics_enabled=False)
|
||||
self.assertEqual(iface.process([2])[0], ['4'])
|
||||
iface = gr.Interface(lambda x: x**2, "slider", "textbox", interpretation="default")
|
||||
iface = gr.Interface(lambda x: x**2, "slider", "textbox", interpretation="default", analytics_enabled=False)
|
||||
scores, alternative_outputs = iface.interpret([2])
|
||||
self.assertEqual(scores, [[-4.0, 200.08163265306123, 812.3265306122449, 1832.7346938775513, 3261.3061224489797,
|
||||
5098.040816326531, 7342.938775510205, 9996.0]])
|
||||
|
@ -2,6 +2,10 @@ from gradio.interface import *
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
import requests
|
||||
import os
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class TestInterface(unittest.TestCase):
|
||||
|
@ -4,7 +4,9 @@ import gradio.test_data
|
||||
from gradio.processing_utils import decode_base64_to_image, encode_array_to_base64
|
||||
from gradio import Interface
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
class TestDefault(unittest.TestCase):
|
||||
def test_default_text(self):
|
||||
|
@ -1,6 +1,11 @@
|
||||
import unittest
|
||||
import gradio as gr
|
||||
from gradio import mix
|
||||
import os
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class TestSeries(unittest.TestCase):
|
||||
def test_in_interface(self):
|
||||
|
@ -8,6 +8,9 @@ import warnings
|
||||
import tempfile
|
||||
from unittest.mock import ANY
|
||||
import urllib.request
|
||||
import os
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "True" # Enables analytics
|
||||
|
||||
|
||||
class TestUser(unittest.TestCase):
|
||||
@ -56,7 +59,7 @@ class TestPort(unittest.TestCase):
|
||||
|
||||
class TestFlaskRoutes(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.io = gr.Interface(lambda x: x, "text", "text")
|
||||
self.io = gr.Interface(lambda x: x, "text", "text", analytics_enabled=False)
|
||||
self.app, _, _ = self.io.launch(prevent_thread_lock=True)
|
||||
self.client = self.app.test_client()
|
||||
|
||||
@ -103,7 +106,7 @@ class TestFlaskRoutes(unittest.TestCase):
|
||||
|
||||
class TestAuthenticatedFlaskRoutes(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.io = gr.Interface(lambda x: x, "text", "text")
|
||||
self.io = gr.Interface(lambda x: x, "text", "text", analytics_enabled=False)
|
||||
self.app, _, _ = self.io.launch(auth=("test", "correct_password"), prevent_thread_lock=True)
|
||||
self.client = self.app.test_client()
|
||||
|
||||
@ -123,7 +126,7 @@ class TestAuthenticatedFlaskRoutes(unittest.TestCase):
|
||||
|
||||
class TestInterfaceCustomParameters(unittest.TestCase):
|
||||
def test_show_error(self):
|
||||
io = gr.Interface(lambda x: 1/x, "number", "number")
|
||||
io = gr.Interface(lambda x: 1/x, "number", "number", analytics_enabled=False)
|
||||
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
||||
client = app.test_client()
|
||||
response = client.post('/api/predict/', json={"data": [0]})
|
||||
@ -132,12 +135,15 @@ class TestInterfaceCustomParameters(unittest.TestCase):
|
||||
io.close()
|
||||
|
||||
def test_feature_logging(self):
|
||||
io = gr.Interface(lambda x: 1/x, "number", "number")
|
||||
io.launch(show_error=True, prevent_thread_lock=True)
|
||||
with mock.patch('requests.post') as mock_post:
|
||||
io = gr.Interface(lambda x: 1/x, "number", "number")
|
||||
io.launch(show_error=True, prevent_thread_lock=True)
|
||||
networking.log_feature_analytics("test_feature")
|
||||
mock_post.assert_called_once_with(networking.GRADIO_FEATURE_ANALYTICS_URL, data=ANY, timeout=ANY)
|
||||
mock_post.assert_called_with(networking.GRADIO_FEATURE_ANALYTICS_URL, data=ANY, timeout=ANY)
|
||||
io.close()
|
||||
|
||||
io = gr.Interface(lambda x: 1/x, "number", "number", analytics_enabled=False)
|
||||
print(io.analytics_enabled)
|
||||
io.launch(show_error=True, prevent_thread_lock=True)
|
||||
with mock.patch('requests.post') as mock_post:
|
||||
networking.log_feature_analytics("test_feature")
|
||||
@ -146,7 +152,7 @@ class TestInterfaceCustomParameters(unittest.TestCase):
|
||||
|
||||
class TestFlagging(unittest.TestCase):
|
||||
def test_num_rows_written(self):
|
||||
io = gr.Interface(lambda x: x, "text", "text")
|
||||
io = gr.Interface(lambda x: x, "text", "text", analytics_enabled=False)
|
||||
io.launch(prevent_thread_lock=True)
|
||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||
row_count = networking.flag_data(["test"], ["test"], flag_path=tmpdirname)
|
||||
@ -155,39 +161,39 @@ class TestFlagging(unittest.TestCase):
|
||||
self.assertEquals(row_count, 2) # 3 rows written including header
|
||||
io.close()
|
||||
|
||||
def test_flagging_analytics(self):
|
||||
@mock.patch("requests.post")
|
||||
@mock.patch("gradio.networking.flag_data")
|
||||
def test_flagging_analytics(self, mock_flag, mock_post):
|
||||
io = gr.Interface(lambda x: x, "text", "text")
|
||||
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
||||
client = app.test_client()
|
||||
with mock.patch('requests.post') as mock_post:
|
||||
with mock.patch('gradio.networking.flag_data') as mock_flag:
|
||||
response = client.post('/api/flag/', json={"data": {"input_data": ["test"], "output_data": ["test"]}})
|
||||
mock_post.assert_called_once()
|
||||
mock_flag.assert_called_once()
|
||||
response = client.post('/api/flag/', json={"data": {"input_data": ["test"], "output_data": ["test"]}})
|
||||
mock_post.assert_any_call(networking.GRADIO_FEATURE_ANALYTICS_URL, data=ANY, timeout=ANY)
|
||||
mock_flag.assert_called_once()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
io.close()
|
||||
|
||||
@mock.patch("requests.post")
|
||||
class TestInterpretation(unittest.TestCase):
|
||||
def test_interpretation(self):
|
||||
def test_interpretation(self, mock_post):
|
||||
io = gr.Interface(lambda x: len(x), "text", "label", interpretation="default")
|
||||
app, _, _ = io.launch(prevent_thread_lock=True)
|
||||
client = app.test_client()
|
||||
io.interpret = mock.MagicMock(return_value=(None, None))
|
||||
with mock.patch('requests.post') as mock_post:
|
||||
response = client.post('/api/interpret/', json={"data": ["test test"]})
|
||||
mock_post.assert_called_once()
|
||||
response = client.post('/api/interpret/', json={"data": ["test test"]})
|
||||
mock_post.assert_any_call(networking.GRADIO_FEATURE_ANALYTICS_URL, data=ANY, timeout=ANY)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
io.close()
|
||||
|
||||
class TestState(unittest.TestCase):
|
||||
def test_state_initialization(self):
|
||||
io = gr.Interface(lambda x: len(x), "text", "label")
|
||||
io = gr.Interface(lambda x: len(x), "text", "label", analytics_enabled=False)
|
||||
app, _, _ = io.launch(prevent_thread_lock=True)
|
||||
with app.test_request_context():
|
||||
self.assertIsNone(networking.get_state())
|
||||
|
||||
def test_state_value(self):
|
||||
io = gr.Interface(lambda x: len(x), "text", "label")
|
||||
io = gr.Interface(lambda x: len(x), "text", "label", analytics_enabled=False)
|
||||
io.launch(prevent_thread_lock=True)
|
||||
app, _, _ = io.launch(prevent_thread_lock=True)
|
||||
with app.test_request_context():
|
||||
@ -214,7 +220,7 @@ class TestURLs(unittest.TestCase):
|
||||
|
||||
class TestQueuing(unittest.TestCase):
|
||||
def test_queueing(self):
|
||||
io = gr.Interface(lambda x: x, "text", "text")
|
||||
io = gr.Interface(lambda x: x, "text", "text", analytics_enabled=False)
|
||||
app, _, _ = io.launch(prevent_thread_lock=True)
|
||||
client = app.test_client()
|
||||
# mock queue methods and post method
|
||||
|
@ -4,6 +4,10 @@ import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class OutputComponent(unittest.TestCase):
|
||||
|
@ -7,6 +7,10 @@ import numpy as np
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class ImagePreprocessing(unittest.TestCase):
|
||||
def test_decode_base64_to_image(self):
|
||||
output_image = gr.processing_utils.decode_base64_to_image(
|
||||
|
@ -6,6 +6,10 @@ import unittest.mock as mock
|
||||
from gradio import tunneling, networking, Interface
|
||||
import threading
|
||||
import paramiko
|
||||
import os
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class TestTunneling(unittest.TestCase):
|
||||
|
@ -4,6 +4,10 @@ import pkg_resources
|
||||
import unittest.mock as mock
|
||||
import warnings
|
||||
import requests
|
||||
import os
|
||||
|
||||
|
||||
os.environ["GRADIO_ANALYTICS_ENABLED"] = "" # Disables analytics
|
||||
|
||||
|
||||
class TestUtils(unittest.TestCase):
|
||||
|
Loading…
Reference in New Issue
Block a user