Spaces:
Sleeping
Sleeping
Download tests/test_app.py from yuntian-deng/implicit-cot-math: direct link, hf CLI and curl.
- Browser
- Download file 3.43 kB
-
https://huggingface.co/spaces/yuntian-deng/implicit-cot-math/resolve/main/tests/test_app.py
- Command line
-
hf download hf://spaces/yuntian-deng/implicit-cot-math/tests/test_app.py
-
curl -L -o test_app.py https://huggingface.co/spaces/yuntian-deng/implicit-cot-math/resolve/main/tests/test_app.py
3.43 kB
| """Test the current runtime without downloading model weights or using a GPU.""" | |
| import importlib.util | |
| from pathlib import Path | |
| import unittest | |
| from unittest.mock import patch | |
| import gradio as gr | |
| import httpx | |
| import torch | |
| from transformers import BatchEncoding, MistralConfig, MistralForCausalLM | |
| class Tokenizer: | |
| eos_token = '</s>' | |
| last_text = None | |
| def __call__(self, text, return_tensors): | |
| self.last_text = text | |
| return BatchEncoding({'input_ids': torch.tensor([[3, 4, 2]]), | |
| 'attention_mask': torch.ones((1, 3), dtype=torch.long)}) | |
| def decode(self, tokens, skip_special_tokens): | |
| return ' '.join(str(int(token)) for token in tokens) | |
| class DemoTests(unittest.TestCase): | |
| def setUpClass(cls): | |
| cls.model = MistralForCausalLM(MistralConfig( | |
| vocab_size=32, hidden_size=16, intermediate_size=32, | |
| num_hidden_layers=1, num_attention_heads=2, num_key_value_heads=1, | |
| max_position_embeddings=128, eos_token_id=None, pad_token_id=0, | |
| )).eval() | |
| cls.tokenizer = Tokenizer() | |
| spec = importlib.util.spec_from_file_location('math_demo', Path(__file__).resolve().parents[1] / 'app.py') | |
| cls.app = importlib.util.module_from_spec(spec) | |
| with patch('transformers.AutoModelForCausalLM.from_pretrained', return_value=cls.model), \ | |
| patch('transformers.AutoTokenizer.from_pretrained', return_value=cls.tokenizer), \ | |
| patch('spaces.GPU', side_effect=lambda fn: fn): | |
| spec.loader.exec_module(cls.app) | |
| def test_blank_input_before_gpu_request(self): | |
| for value in ('', ' \n', None): | |
| with patch.object(self.app, 'generate_answer') as generate: | |
| with self.assertRaises(gr.Error): | |
| self.app.predict_answer(value) | |
| generate.assert_not_called() | |
| def test_real_mistral_generation_and_prompt(self): | |
| answer = self.app.predict_answer(' How\n many? ') | |
| self.assertEqual(self.tokenizer.last_text, 'How many? </s>') | |
| self.assertEqual(len(answer.split()), 10) | |
| def test_errors_are_not_answers(self): | |
| with patch.object(self.app, 'generate_answer', side_effect=RuntimeError('test')), \ | |
| patch.object(self.app.logger, 'exception') as log: | |
| with self.assertRaises(gr.Error): | |
| self.app.predict_answer('How many?') | |
| log.assert_called_once() | |
| def test_preserve_quota_errors(self): | |
| error = gr.Error('ZeroGPU quota exceeded') | |
| with patch.object(self.app, 'generate_answer', side_effect=error): | |
| with self.assertRaises(gr.Error) as raised: | |
| self.app.predict_answer('How many?') | |
| self.assertIs(raised.exception, error) | |
| def test_real_web_ui(self): | |
| _, url, _ = self.app.demo.queue().launch( | |
| prevent_thread_lock=True, server_name='127.0.0.1', quiet=True, share=False) | |
| try: | |
| with httpx.Client(base_url=url, timeout=15) as client: | |
| for path in ('/', '/config', '/gradio_api/info'): | |
| self.assertEqual(client.get(path).status_code, 200) | |
| endpoints = client.get('/gradio_api/info').json()['named_endpoints'] | |
| self.assertIn('/predict_answer', endpoints) | |
| finally: | |
| self.app.demo.close() | |
| if __name__ == '__main__': | |
| unittest.main() | |